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

247 lines
14 KiB
Java
Raw Normal View History

2020-04-13 21:48:35 +02:00
package de.ellpeck.prettypipes;
2020-04-20 03:37:23 +02:00
import de.ellpeck.prettypipes.entities.PipeFrameEntity;
import de.ellpeck.prettypipes.entities.PipeFrameRenderer;
import de.ellpeck.prettypipes.items.*;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
2020-10-14 04:28:58 +02:00
import de.ellpeck.prettypipes.pipe.modules.FilterModifierModuleItem;
2020-04-17 17:18:25 +02:00
import de.ellpeck.prettypipes.pipe.modules.LowPriorityModuleItem;
2020-05-03 15:51:16 +02:00
import de.ellpeck.prettypipes.pipe.modules.RedstoneModuleItem;
2020-04-16 23:40:35 +02:00
import de.ellpeck.prettypipes.pipe.modules.SpeedModuleItem;
2020-10-14 22:04:52 +02:00
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleGui;
import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleItem;
2020-04-16 23:40:35 +02:00
import de.ellpeck.prettypipes.pipe.modules.extraction.ExtractionModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.extraction.ExtractionModuleGui;
import de.ellpeck.prettypipes.pipe.modules.extraction.ExtractionModuleItem;
2020-04-14 04:21:28 +02:00
import de.ellpeck.prettypipes.network.PipeNetwork;
2020-04-14 17:14:24 +02:00
import de.ellpeck.prettypipes.packets.PacketHandler;
2020-04-16 04:42:42 +02:00
import de.ellpeck.prettypipes.pipe.*;
import de.ellpeck.prettypipes.pipe.containers.*;
2020-10-14 04:28:58 +02:00
import de.ellpeck.prettypipes.pipe.modules.filter.FilterIncreaseModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.filter.FilterIncreaseModuleGui;
import de.ellpeck.prettypipes.pipe.modules.filter.FilterIncreaseModuleItem;
2020-04-16 23:40:35 +02:00
import de.ellpeck.prettypipes.pipe.modules.insertion.FilterModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.insertion.FilterModuleGui;
import de.ellpeck.prettypipes.pipe.modules.insertion.FilterModuleItem;
2020-04-17 20:03:54 +02:00
import de.ellpeck.prettypipes.pipe.modules.retrieval.RetrievalModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.retrieval.RetrievalModuleGui;
import de.ellpeck.prettypipes.pipe.modules.retrieval.RetrievalModuleItem;
2020-04-18 22:30:14 +02:00
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleContainer;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleGui;
import de.ellpeck.prettypipes.pipe.modules.stacksize.StackSizeModuleItem;
2020-10-13 18:11:40 +02:00
import de.ellpeck.prettypipes.pressurizer.PressurizerBlock;
import de.ellpeck.prettypipes.pressurizer.PressurizerContainer;
import de.ellpeck.prettypipes.pressurizer.PressurizerGui;
import de.ellpeck.prettypipes.pressurizer.PressurizerTileEntity;
2020-05-08 22:58:16 +02:00
import de.ellpeck.prettypipes.terminal.CraftingTerminalBlock;
import de.ellpeck.prettypipes.terminal.CraftingTerminalTileEntity;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.terminal.ItemTerminalBlock;
import de.ellpeck.prettypipes.terminal.ItemTerminalTileEntity;
2020-05-08 22:58:16 +02:00
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalGui;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalContainer;
import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui;
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-20 03:37:23 +02:00
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntityType;
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.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;
2020-04-14 17:14:24 +02:00
import net.minecraftforge.fml.client.registry.ClientRegistry;
2020-04-20 03:37:23 +02:00
import net.minecraftforge.fml.client.registry.RenderingRegistry;
2020-04-13 21:48:35 +02:00
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-16 00:39:53 +02:00
import net.minecraftforge.registries.IForgeRegistry;
2020-04-13 21:48:35 +02:00
2020-04-14 04:21:28 +02:00
import javax.annotation.Nullable;
2020-04-16 00:39:53 +02:00
import java.util.ArrayList;
2020-05-02 18:56:54 +02:00
import java.util.Arrays;
2020-04-16 00:39:53 +02:00
import java.util.List;
import java.util.Locale;
2020-04-16 15:45:11 +02:00
import java.util.function.BiFunction;
2020-04-14 04:21:28 +02:00
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-10-17 14:29:37 +02:00
@CapabilityInject(IPipeConnectable.class)
public static Capability<IPipeConnectable> pipeConnectableCapability;
2020-04-14 04:21:28 +02:00
2020-04-14 01:38:48 +02:00
public static Item wrenchItem;
2020-04-20 13:12:26 +02:00
public static Item pipeFrameItem;
2020-04-14 01:38:48 +02:00
public static Block pipeBlock;
public static TileEntityType<PipeTileEntity> pipeTileEntity;
2020-05-07 18:30:40 +02:00
public static ContainerType<MainPipeContainer> pipeContainer;
public static Block itemTerminalBlock;
public static TileEntityType<ItemTerminalTileEntity> itemTerminalTileEntity;
public static ContainerType<ItemTerminalContainer> itemTerminalContainer;
2020-04-16 04:42:42 +02:00
2020-05-08 22:58:16 +02:00
public static Block craftingTerminalBlock;
public static TileEntityType<CraftingTerminalTileEntity> craftingTerminalTileEntity;
public static ContainerType<CraftingTerminalContainer> craftingTerminalContainer;
2020-04-20 03:37:23 +02:00
public static EntityType<PipeFrameEntity> pipeFrameEntity;
2020-10-13 18:11:40 +02:00
public static Block pressurizerBlock;
public static TileEntityType<PressurizerTileEntity> pressurizerTileEntity;
public static ContainerType<PressurizerContainer> pressurizerContainer;
2020-04-16 04:42:42 +02:00
public static ContainerType<ExtractionModuleContainer> extractionModuleContainer;
2020-04-16 21:19:40 +02:00
public static ContainerType<FilterModuleContainer> filterModuleContainer;
2020-04-17 20:03:54 +02:00
public static ContainerType<RetrievalModuleContainer> retrievalModuleContainer;
2020-04-18 22:30:14 +02:00
public static ContainerType<StackSizeModuleContainer> stackSizeModuleContainer;
2020-10-14 04:28:58 +02:00
public static ContainerType<FilterIncreaseModuleContainer> filterIncreaseModuleContainer;
2020-10-14 22:04:52 +02:00
public static ContainerType<CraftingModuleContainer> craftingModuleContainer;
2020-04-13 21:48:35 +02:00
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event) {
event.getRegistry().registerAll(
2020-05-07 18:30:40 +02:00
pipeBlock = new PipeBlock().setRegistryName("pipe"),
2020-05-08 22:58:16 +02:00
itemTerminalBlock = new ItemTerminalBlock().setRegistryName("item_terminal"),
2020-10-13 18:11:40 +02:00
craftingTerminalBlock = new CraftingTerminalBlock().setRegistryName("crafting_terminal"),
pressurizerBlock = new PressurizerBlock().setRegistryName("pressurizer")
2020-04-13 21:48:35 +02:00
);
}
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event) {
2020-04-16 00:39:53 +02:00
IForgeRegistry<Item> registry = event.getRegistry();
registry.registerAll(
2020-04-17 23:17:46 +02:00
wrenchItem = new WrenchItem().setRegistryName("wrench"),
2020-04-20 03:37:23 +02:00
new Item(new Item.Properties().group(GROUP)).setRegistryName("blank_module"),
2020-04-20 13:12:26 +02:00
pipeFrameItem = new PipeFrameItem().setRegistryName("pipe_frame")
2020-04-13 22:54:18 +02:00
);
2020-04-16 00:39:53 +02:00
registry.registerAll(createTieredModule("extraction_module", ExtractionModuleItem::new));
2020-04-16 21:19:40 +02:00
registry.registerAll(createTieredModule("filter_module", FilterModuleItem::new));
2020-04-16 23:40:35 +02:00
registry.registerAll(createTieredModule("speed_module", SpeedModuleItem::new));
2020-04-17 17:18:25 +02:00
registry.registerAll(createTieredModule("low_priority_module", LowPriorityModuleItem::new));
2020-04-17 20:03:54 +02:00
registry.registerAll(createTieredModule("retrieval_module", RetrievalModuleItem::new));
2020-04-18 22:30:14 +02:00
registry.register(new StackSizeModuleItem("stack_size_module"));
2020-10-14 04:28:58 +02:00
registry.registerAll(Arrays.stream(ItemEqualityType.values()).map(t -> new FilterModifierModuleItem(t.name().toLowerCase(Locale.ROOT) + "_filter_modifier", t)).toArray(Item[]::new));
2020-05-03 15:51:16 +02:00
registry.register(new RedstoneModuleItem("redstone_module"));
2020-10-14 04:28:58 +02:00
registry.register(new FilterIncreaseModuleItem("filter_increase_modifier"));
2020-10-14 22:04:52 +02:00
registry.registerAll(createTieredModule("crafting_module", CraftingModuleItem::new));
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-16 00:39:53 +02:00
.forEach(b -> registry.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(
2020-05-07 18:30:40 +02:00
pipeTileEntity = (TileEntityType<PipeTileEntity>) TileEntityType.Builder.create(PipeTileEntity::new, pipeBlock).build(null).setRegistryName("pipe"),
2020-05-08 22:58:16 +02:00
itemTerminalTileEntity = (TileEntityType<ItemTerminalTileEntity>) TileEntityType.Builder.create(ItemTerminalTileEntity::new, itemTerminalBlock).build(null).setRegistryName("item_terminal"),
2020-10-13 18:11:40 +02:00
craftingTerminalTileEntity = (TileEntityType<CraftingTerminalTileEntity>) TileEntityType.Builder.create(CraftingTerminalTileEntity::new, craftingTerminalBlock).build(null).setRegistryName("crafting_terminal"),
pressurizerTileEntity = (TileEntityType<PressurizerTileEntity>) TileEntityType.Builder.create(PressurizerTileEntity::new, pressurizerBlock).build(null).setRegistryName("pressurizer")
2020-04-14 01:38:48 +02:00
);
}
@SubscribeEvent
2020-04-20 03:37:23 +02:00
public static void registerEntities(RegistryEvent.Register<EntityType<?>> event) {
event.getRegistry().registerAll(
pipeFrameEntity = (EntityType<PipeFrameEntity>) EntityType.Builder.<PipeFrameEntity>create(PipeFrameEntity::new, EntityClassification.MISC).build("pipe_frame").setRegistryName("pipe_frame")
);
}
@SubscribeEvent
public static void registerContainers(RegistryEvent.Register<ContainerType<?>> event) {
2020-04-14 01:38:48 +02:00
event.getRegistry().registerAll(
2020-04-16 04:42:42 +02:00
pipeContainer = (ContainerType<MainPipeContainer>) IForgeContainerType.create((windowId, inv, data) -> new MainPipeContainer(pipeContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pipe"),
2020-05-07 18:30:40 +02:00
itemTerminalContainer = (ContainerType<ItemTerminalContainer>) IForgeContainerType.create((windowId, inv, data) -> new ItemTerminalContainer(itemTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("item_terminal"),
2020-05-08 22:58:16 +02:00
craftingTerminalContainer = (ContainerType<CraftingTerminalContainer>) IForgeContainerType.create((windowId, inv, data) -> new CraftingTerminalContainer(craftingTerminalContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("crafting_terminal"),
2020-10-13 18:11:40 +02:00
pressurizerContainer = (ContainerType<PressurizerContainer>) IForgeContainerType.create((windowId, inv, data) -> new PressurizerContainer(pressurizerContainer, windowId, inv.player, data.readBlockPos())).setRegistryName("pressurizer"),
2020-04-17 20:03:54 +02:00
extractionModuleContainer = createPipeContainer("extraction_module"),
filterModuleContainer = createPipeContainer("filter_module"),
2020-04-18 22:30:14 +02:00
retrievalModuleContainer = createPipeContainer("retrieval_module"),
2020-10-14 04:28:58 +02:00
stackSizeModuleContainer = createPipeContainer("stack_size_module"),
2020-10-14 22:04:52 +02:00
filterIncreaseModuleContainer = createPipeContainer("filter_increase_module"),
craftingModuleContainer = createPipeContainer("crafting_module")
2020-04-16 21:19:40 +02:00
);
2020-04-16 00:39:53 +02:00
}
private static <T extends AbstractPipeContainer<?>> ContainerType<T> createPipeContainer(String name) {
2020-04-17 20:03:54 +02:00
return (ContainerType<T>) IForgeContainerType.create((windowId, inv, data) -> {
PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, inv.player.world, data.readBlockPos());
int moduleIndex = data.readInt();
ItemStack moduleStack = tile.modules.getStackInSlot(moduleIndex);
return ((IModule) moduleStack.getItem()).getContainer(moduleStack, tile, windowId, inv, inv.player, moduleIndex);
}).setRegistryName(name);
}
private static Item[] createTieredModule(String name, BiFunction<String, ModuleTier, ModuleItem> item) {
2020-04-18 22:30:14 +02:00
List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values())
items.add(item.apply(name, tier).setRegistryName(tier.name().toLowerCase(Locale.ROOT) + "_" + name));
return items.toArray(new Item[0]);
}
2020-04-13 21:48:35 +02:00
public static void setup(FMLCommonSetupEvent event) {
2020-10-17 14:29:37 +02:00
registerCap(PipeNetwork.class);
registerCap(IPipeConnectable.class);
2020-04-14 17:14:24 +02:00
PacketHandler.setup();
2020-04-13 21:48:35 +02:00
}
2020-04-16 04:42:42 +02:00
public static final class Client {
public static void setup(FMLClientSetupEvent event) {
2020-09-22 19:14:07 +02:00
RenderTypeLookup.setRenderLayer(pipeBlock, RenderType.getCutout());
2020-04-16 04:42:42 +02:00
ClientRegistry.bindTileEntityRenderer(pipeTileEntity, PipeRenderer::new);
2020-04-20 03:37:23 +02:00
RenderingRegistry.registerEntityRenderingHandler(pipeFrameEntity, PipeFrameRenderer::new);
2020-04-16 04:42:42 +02:00
ScreenManager.registerFactory(pipeContainer, MainPipeGui::new);
2020-05-07 18:30:40 +02:00
ScreenManager.registerFactory(itemTerminalContainer, ItemTerminalGui::new);
2020-10-13 18:11:40 +02:00
ScreenManager.registerFactory(pressurizerContainer, PressurizerGui::new);
2020-05-08 22:58:16 +02:00
ScreenManager.registerFactory(craftingTerminalContainer, CraftingTerminalGui::new);
2020-04-16 04:42:42 +02:00
ScreenManager.registerFactory(extractionModuleContainer, ExtractionModuleGui::new);
2020-04-16 21:19:40 +02:00
ScreenManager.registerFactory(filterModuleContainer, FilterModuleGui::new);
2020-04-17 20:03:54 +02:00
ScreenManager.registerFactory(retrievalModuleContainer, RetrievalModuleGui::new);
2020-04-18 22:30:14 +02:00
ScreenManager.registerFactory(stackSizeModuleContainer, StackSizeModuleGui::new);
2020-10-14 04:28:58 +02:00
ScreenManager.registerFactory(filterIncreaseModuleContainer, FilterIncreaseModuleGui::new);
2020-10-14 22:04:52 +02:00
ScreenManager.registerFactory(craftingModuleContainer, CraftingModuleGui::new);
2020-04-16 04:42:42 +02:00
}
2020-04-13 21:48:35 +02:00
}
2020-10-17 14:29:37 +02:00
private static <T> void registerCap(Class<T> capClass) {
CapabilityManager.INSTANCE.register(capClass, new Capability.IStorage<T>() {
@Nullable
@Override
public INBT writeNBT(Capability<T> capability, T instance, Direction side) {
return null;
}
@Override
public void readNBT(Capability<T> capability, T instance, Direction side, INBT nbt) {
}
}, () -> null);
}
2020-04-13 21:48:35 +02:00
}