Search in sources :

Example 1 with TextTable

use of net.minecraftforge.common.util.TextTable 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);
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) TextTable(net.minecraftforge.common.util.TextTable) Items(net.minecraft.world.item.Items) FluidHandlerItemStackSimple(net.minecraftforge.fluids.capability.templates.FluidHandlerItemStackSimple) Stopwatch(com.google.common.base.Stopwatch) TextTable.column(net.minecraftforge.common.util.TextTable.column) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Field(java.lang.reflect.Field) IEventBus(net.minecraftforge.eventbus.api.IEventBus) ArrayList(java.util.ArrayList) FMLJavaModLoadingContext(net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MinecraftForge(net.minecraftforge.common.MinecraftForge) List(java.util.List) Lists(com.google.common.collect.Lists) FMLCommonSetupEvent(net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent) Logger(org.apache.logging.log4j.Logger) Mod(net.minecraftforge.fml.common.Mod) ItemStack(net.minecraft.world.item.ItemStack) CapabilityProvider(net.minecraftforge.common.capabilities.CapabilityProvider) LogManager(org.apache.logging.log4j.LogManager) Stopwatch(com.google.common.base.Stopwatch) FluidHandlerItemStackSimple(net.minecraftforge.fluids.capability.templates.FluidHandlerItemStackSimple) AttachCapabilitiesEvent(net.minecraftforge.event.AttachCapabilitiesEvent) Field(java.lang.reflect.Field) ResourceLocation(net.minecraft.resources.ResourceLocation) TextTable(net.minecraftforge.common.util.TextTable) ItemStack(net.minecraft.world.item.ItemStack) IEventBus(net.minecraftforge.eventbus.api.IEventBus)

Example 2 with TextTable

use of net.minecraftforge.common.util.TextTable in project MinecraftForge by MinecraftForge.

the class TextTableTest method testMarkdownCompliance.

@Test
public void testMarkdownCompliance() {
    TextTable table = new TextTable(Lists.newArrayList(column("Left", TextTable.Alignment.LEFT), column("Center", TextTable.Alignment.CENTER), column("Right", TextTable.Alignment.RIGHT)));
    table.add("Long Value 1", "Value 2", "Value 3");
    table.add("Value 1", "Long Value 2", "Value 3");
    table.add("Value 1", "Value 2", "Long Value 3");
    int[] columnWidths = table.getColumns().stream().mapToInt(Column::getWidth).toArray();
    assertArrayEquals(new int[] { 12, 12, 12 }, columnWidths, "Column widths should adjust for long values");
    String[] result = table.build("\n").split("\n");
    assertEquals(5, result.length, "Header row + separator row + value rows should result in 5 lines");
    assertEquals("| Left         |    Center    |        Right |", result[0], "Column headers should be properly formatted");
    assertEquals("|:------------ |:------------:| ------------:|", result[1], "Header-body separators should contain markdown alignment information");
}
Also used : TextTable(net.minecraftforge.common.util.TextTable) Test(org.junit.jupiter.api.Test)

Aggregations

TextTable (net.minecraftforge.common.util.TextTable)2 Stopwatch (com.google.common.base.Stopwatch)1 Lists (com.google.common.collect.Lists)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 ItemStack (net.minecraft.world.item.ItemStack)1 Items (net.minecraft.world.item.Items)1 MinecraftForge (net.minecraftforge.common.MinecraftForge)1 CapabilityProvider (net.minecraftforge.common.capabilities.CapabilityProvider)1 TextTable.column (net.minecraftforge.common.util.TextTable.column)1 AttachCapabilitiesEvent (net.minecraftforge.event.AttachCapabilitiesEvent)1 IEventBus (net.minecraftforge.eventbus.api.IEventBus)1 FluidHandlerItemStackSimple (net.minecraftforge.fluids.capability.templates.FluidHandlerItemStackSimple)1 Mod (net.minecraftforge.fml.common.Mod)1 FMLCommonSetupEvent (net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent)1 FMLJavaModLoadingContext (net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext)1