Search in sources :

Example 1 with FMLCommonSetupEvent

use of net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent in project Overloaded by CJ-MC-Mods.

the class CommonProxy method commonSetup.

public void commonSetup(FMLCommonSetupEvent event) {
    CapabilityHyperItem.register();
    CapabilityHyperEnergy.register();
    CapabilityHyperFluid.register();
    CapabilityGenericDataStorage.register();
    networkWrapper = newSimpleChannel(ResourceLocation.of("overloaded_network", '_'), () -> "1.0", v -> true, v -> true);
    int dis = 0;
    networkWrapper.registerMessage(dis++, LeftClickBlockMessage.class, LeftClickBlockMessage::toBytes, LeftClickBlockMessage::fromBytes, new PlayerMessageHandler<>(ItemMultiTool::leftClickOnBlockServer));
    networkWrapper.registerMessage(dis++, RightClickBlockMessage.class, RightClickBlockMessage::toBytes, RightClickBlockMessage::fromBytes, new PlayerMessageHandler<>(ModItems.multiTool::rightClickWithItem));
    networkWrapper.registerMessage(dis++, RayGunMessage.class, RayGunMessage::toBytes, RayGunMessage::fromBytes, new PlayerMessageHandler<>(ModItems.rayGun::handleMessage));
    networkWrapper.registerMessage(dis++, MultiArmorSettingsMessage.class, MultiArmorSettingsMessage::toBytes, MultiArmorSettingsMessage::fromBytes, new PlayerMessageHandler<>(ModItems.customHelmet::updateSettings));
    networkWrapper.registerMessage(dis++, RailGunFireMessage.class, RailGunFireMessage::toBytes, RailGunFireMessage::fromBytes, new PlayerMessageHandler<>(ModItems.railgun::handleFireMessage));
    networkWrapper.registerMessage(dis++, RailGunSettingsMessage.class, RailGunSettingsMessage::toBytes, RailGunSettingsMessage::fromBytes, new PlayerMessageHandler<>(ModItems.railgun::handleSettingsMessage));
    networkWrapper.registerMessage(dis++, KeyBindPressedMessage.class, KeyBindPressedMessage::toBytes, KeyBindPressedMessage::fromBytes, new KeyBindPressedHandler());
    networkWrapper.registerMessage(dis++, NoClipStatusMessage.class, NoClipStatusMessage::toBytes, NoClipStatusMessage::fromBytes, new NoClipUpdateHandler());
    networkWrapper.registerMessage(dis++, ContainerDataMessage.class, ContainerDataMessage::toBytes, ContainerDataMessage::fromBytes, new ContainerDataHandler());
    MinecraftForge.EVENT_BUS.register(new ArmorEventHandler());
}
Also used : Item(net.minecraft.item.Item) NoClipUpdateHandler(com.cjm721.overloaded.network.handler.NoClipUpdateHandler) KeyBindPressedHandler(com.cjm721.overloaded.network.handler.KeyBindPressedHandler) ContainerType(net.minecraft.inventory.container.ContainerType) FMLJavaModLoadingContext(net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext) ModBlocks(com.cjm721.overloaded.block.ModBlocks) FMLCommonSetupEvent(net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent) CapabilityGenericDataStorage(com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage) ModContainers(com.cjm721.overloaded.network.container.ModContainers) Block(net.minecraft.block.Block) TileEntityType(net.minecraft.tileentity.TileEntityType) RegistryEvent(net.minecraftforge.event.RegistryEvent) CapabilityHyperFluid(com.cjm721.overloaded.capabilities.CapabilityHyperFluid) LinkedList(java.util.LinkedList) NetworkRegistry.newSimpleChannel(net.minecraftforge.fml.network.NetworkRegistry.newSimpleChannel) com.cjm721.overloaded.network.packets(com.cjm721.overloaded.network.packets) ModItems(com.cjm721.overloaded.item.ModItems) ModTiles(com.cjm721.overloaded.tile.ModTiles) CapabilityHyperItem(com.cjm721.overloaded.capabilities.CapabilityHyperItem) ModFluids(com.cjm721.overloaded.fluid.ModFluids) MinecraftForge(net.minecraftforge.common.MinecraftForge) List(java.util.List) CapabilityHyperEnergy(com.cjm721.overloaded.capabilities.CapabilityHyperEnergy) ContainerDataHandler(com.cjm721.overloaded.network.handler.ContainerDataHandler) SimpleChannel(net.minecraftforge.fml.network.simple.SimpleChannel) PlayerMessageHandler(com.cjm721.overloaded.network.handler.PlayerMessageHandler) ResourceLocation(net.minecraft.util.ResourceLocation) ItemMultiTool(com.cjm721.overloaded.item.functional.ItemMultiTool) ArmorEventHandler(com.cjm721.overloaded.item.functional.armor.ArmorEventHandler) Fluid(net.minecraft.fluid.Fluid) NoClipUpdateHandler(com.cjm721.overloaded.network.handler.NoClipUpdateHandler) KeyBindPressedHandler(com.cjm721.overloaded.network.handler.KeyBindPressedHandler) ArmorEventHandler(com.cjm721.overloaded.item.functional.armor.ArmorEventHandler) ModItems(com.cjm721.overloaded.item.ModItems) ContainerDataHandler(com.cjm721.overloaded.network.handler.ContainerDataHandler)

Example 2 with FMLCommonSetupEvent

use of net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent 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)

Aggregations

List (java.util.List)2 MinecraftForge (net.minecraftforge.common.MinecraftForge)2 FMLCommonSetupEvent (net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent)2 FMLJavaModLoadingContext (net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext)2 ModBlocks (com.cjm721.overloaded.block.ModBlocks)1 CapabilityGenericDataStorage (com.cjm721.overloaded.capabilities.CapabilityGenericDataStorage)1 CapabilityHyperEnergy (com.cjm721.overloaded.capabilities.CapabilityHyperEnergy)1 CapabilityHyperFluid (com.cjm721.overloaded.capabilities.CapabilityHyperFluid)1 CapabilityHyperItem (com.cjm721.overloaded.capabilities.CapabilityHyperItem)1 ModFluids (com.cjm721.overloaded.fluid.ModFluids)1 ModItems (com.cjm721.overloaded.item.ModItems)1 ItemMultiTool (com.cjm721.overloaded.item.functional.ItemMultiTool)1 ArmorEventHandler (com.cjm721.overloaded.item.functional.armor.ArmorEventHandler)1 ModContainers (com.cjm721.overloaded.network.container.ModContainers)1 ContainerDataHandler (com.cjm721.overloaded.network.handler.ContainerDataHandler)1 KeyBindPressedHandler (com.cjm721.overloaded.network.handler.KeyBindPressedHandler)1 NoClipUpdateHandler (com.cjm721.overloaded.network.handler.NoClipUpdateHandler)1 PlayerMessageHandler (com.cjm721.overloaded.network.handler.PlayerMessageHandler)1 com.cjm721.overloaded.network.packets (com.cjm721.overloaded.network.packets)1 ModTiles (com.cjm721.overloaded.tile.ModTiles)1