PrettyPipes/src/main/java/de/ellpeck/prettypipes/Registry.java

113 lines
4.7 KiB
Java
Raw Normal View History

2020-04-13 21:48:35 +02:00
package de.ellpeck.prettypipes;
2020-04-14 01:38:48 +02:00
import de.ellpeck.prettypipes.blocks.pipe.PipeBlock;
import de.ellpeck.prettypipes.blocks.pipe.PipeContainer;
import de.ellpeck.prettypipes.blocks.pipe.PipeGui;
import de.ellpeck.prettypipes.blocks.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.items.ExtractionUpgradeItem;
2020-04-13 22:54:18 +02:00
import de.ellpeck.prettypipes.items.WrenchItem;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.network.PipeNetwork;
2020-04-13 21:48:35 +02:00
import net.minecraft.block.Block;
2020-04-14 01:38:48 +02:00
import net.minecraft.client.gui.ScreenManager;
2020-04-13 21:48:35 +02:00
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
2020-04-14 01:38:48 +02:00
import net.minecraft.inventory.container.ContainerType;
2020-04-13 21:48:35 +02:00
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
2020-04-13 22:54:18 +02:00
import net.minecraft.item.ItemStack;
2020-04-14 04:21:28 +02:00
import net.minecraft.nbt.INBT;
2020-04-14 01:38:48 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
2020-04-14 04:21:28 +02:00
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;
2020-04-14 01:38:48 +02:00
import net.minecraftforge.common.extensions.IForgeContainerType;
2020-04-13 21:48:35 +02:00
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.registries.ForgeRegistries;
2020-04-14 04:21:28 +02:00
import javax.annotation.Nullable;
2020-04-13 21:48:35 +02:00
@Mod.EventBusSubscriber(bus = Bus.MOD)
public final class Registry {
2020-04-13 22:54:18 +02:00
public static final ItemGroup GROUP = new ItemGroup(PrettyPipes.ID) {
@Override
public ItemStack createIcon() {
2020-04-14 01:38:48 +02:00
return new ItemStack(wrenchItem);
2020-04-13 22:54:18 +02:00
}
};
2020-04-14 04:21:28 +02:00
@CapabilityInject(PipeNetwork.class)
public static Capability<PipeNetwork> pipeNetworkCapability;
2020-04-14 01:38:48 +02:00
public static Item wrenchItem;
public static Item extractionUpgradeItem;
public static Block pipeBlock;
public static TileEntityType<PipeTileEntity> pipeTileEntity;
public static ContainerType<PipeContainer> pipeContainer;
2020-04-13 21:48:35 +02:00
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event) {
event.getRegistry().registerAll(
2020-04-14 01:38:48 +02:00
pipeBlock = new PipeBlock().setRegistryName("pipe")
2020-04-13 21:48:35 +02:00
);
}
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event) {
2020-04-13 22:54:18 +02:00
event.getRegistry().registerAll(
2020-04-14 01:38:48 +02:00
wrenchItem = new WrenchItem().setRegistryName("wrench"),
extractionUpgradeItem = new ExtractionUpgradeItem().setRegistryName("extraction_upgrade")
2020-04-13 22:54:18 +02:00
);
2020-04-13 21:48:35 +02:00
ForgeRegistries.BLOCKS.getValues().stream()
.filter(b -> b.getRegistryName().getNamespace().equals(PrettyPipes.ID))
2020-04-13 22:54:18 +02:00
.forEach(b -> event.getRegistry().register(new BlockItem(b, new Item.Properties().group(GROUP)).setRegistryName(b.getRegistryName())));
2020-04-13 21:48:35 +02:00
}
2020-04-14 01:38:48 +02:00
@SubscribeEvent
public static void registerTiles(RegistryEvent.Register<TileEntityType<?>> event) {
event.getRegistry().registerAll(
pipeTileEntity = (TileEntityType<PipeTileEntity>) TileEntityType.Builder.create(PipeTileEntity::new, pipeBlock).build(null).setRegistryName("pipe")
);
}
@SubscribeEvent
public static void registerContainer(RegistryEvent.Register<ContainerType<?>> event) {
event.getRegistry().registerAll(
pipeContainer = (ContainerType<PipeContainer>) IForgeContainerType.create((windowId, inv, data) -> {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, inv.player.world, data.readBlockPos());
return tile != null ? new PipeContainer(pipeContainer, windowId, inv.player, tile) : null;
}).setRegistryName("pipe")
);
}
2020-04-13 21:48:35 +02:00
public static void setup(FMLCommonSetupEvent event) {
2020-04-14 04:21:28 +02:00
CapabilityManager.INSTANCE.register(PipeNetwork.class, new Capability.IStorage<PipeNetwork>() {
@Nullable
@Override
public INBT writeNBT(Capability<PipeNetwork> capability, PipeNetwork instance, Direction side) {
return null;
}
@Override
public void readNBT(Capability<PipeNetwork> capability, PipeNetwork instance, Direction side, INBT nbt) {
2020-04-13 21:48:35 +02:00
2020-04-14 04:21:28 +02:00
}
}, () -> null);
2020-04-13 21:48:35 +02:00
}
public static void setupClient(FMLClientSetupEvent event) {
2020-04-14 01:38:48 +02:00
RenderTypeLookup.setRenderLayer(pipeBlock, RenderType.cutout());
ScreenManager.registerFactory(pipeContainer, PipeGui::new);
2020-04-13 21:48:35 +02:00
}
}