use of net.minecraftforge.fluids.capability.templates.FluidHandlerItemStackSimple in project MinecraftForge by MinecraftForge.
the class LazyCapabilitiesOnItemsTest method onCommonSetup.
private void onCommonSetup(FMLCommonSetupEvent event) {
try {
final Field supportsFlagField = CapabilityProvider.class.getDeclaredField("SUPPORTS_LAZY_CAPABILITIES");
supportsFlagField.setAccessible(true);
supportsFlagField.set(null, false);
final Stopwatch timer = Stopwatch.createUnstarted();
final IEventBus bus = MinecraftForge.EVENT_BUS;
final ResourceLocation testCapId = new ResourceLocation(MOD_ID, "test");
final Consumer<AttachCapabilitiesEvent<ItemStack>> capAttachmentHandler = e -> {
// Example capability we make everything a bucket :D
e.addCapability(testCapId, new FluidHandlerItemStackSimple(e.getObject(), SAMPLE_SIZE));
};
// Warmup:
for (int i = 0; i < (SAMPLE_SIZE); i++) {
WARMUP_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
// /First test: SAMPLE_SIZE itemstacks which do not have a capability attached.
timer.start();
for (int i = 0; i < SAMPLE_SIZE; i++) {
NO_CAPS_DISABLED_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.stop();
final long simpleNoCapsLazyDisabledElapsed = timer.elapsed(TimeUnit.MICROSECONDS);
timer.reset();
// /Second test: SAMPLE_SIZE itemstacks with a capability attached.
bus.addGenericListener(ItemStack.class, capAttachmentHandler);
// Warmup:
for (int i = 0; i < (SAMPLE_SIZE); i++) {
WARMUP_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.start();
for (int i = 0; i < SAMPLE_SIZE; i++) {
WITH_CAPS_DISABLED_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.stop();
final long withCapsLazyDisabledElapsed = timer.elapsed(TimeUnit.MICROSECONDS);
timer.reset();
bus.unregister(capAttachmentHandler);
// /Third test: SAMPLE_SIZE itemstacks which do not have a capability attached.
supportsFlagField.set(null, true);
// Warmup:
for (int i = 0; i < (SAMPLE_SIZE); i++) {
WARMUP_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.start();
for (int i = 0; i < SAMPLE_SIZE; i++) {
NO_CAPS_ENABLED_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.stop();
final long simpleNoCapsLazyEnabledElapsed = timer.elapsed(TimeUnit.MICROSECONDS);
timer.reset();
// /Fourth test: SAMPLE_SIZE itemstacks with a capability attached.
bus.addGenericListener(ItemStack.class, capAttachmentHandler);
// Warmup:
for (int i = 0; i < (SAMPLE_SIZE); i++) {
WARMUP_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.start();
for (int i = 0; i < SAMPLE_SIZE; i++) {
WITH_CAPS_ENABLED_RESULTS.add(new ItemStack(Items.WATER_BUCKET));
}
timer.stop();
final long withCapsLazyEnabledElapsed = timer.elapsed(TimeUnit.MICROSECONDS);
timer.reset();
bus.unregister(capAttachmentHandler);
TextTable table = new TextTable(Lists.newArrayList(column("Test type", TextTable.Alignment.LEFT), column("Total time", TextTable.Alignment.CENTER)));
table.add("Lazy: Disabled / Caps: None", simpleNoCapsLazyDisabledElapsed + " ms.");
table.add("Lazy: Disabled / Caps: One", withCapsLazyDisabledElapsed + " ms.");
table.add("Lazy: Enabled / Caps: None", simpleNoCapsLazyEnabledElapsed + " ms.");
table.add("Lazy: Enabled / Caps: One", withCapsLazyEnabledElapsed + " ms.");
final String[] resultData = table.build("\n").split("\n");
for (final String line : resultData) {
LOGGER.warn(line);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
LOGGER.error("Failed to run capabilities on items test!");
throw new IllegalStateException(e);
}
}
Aggregations