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

131 lines
5.5 KiB
Java
Raw Normal View History

2020-04-13 21:48:35 +02:00
package de.ellpeck.prettypipes;
2020-04-16 00:39:53 +02:00
import com.mojang.datafixers.types.Func;
2020-04-14 17:14:24 +02:00
import de.ellpeck.prettypipes.blocks.pipe.*;
2020-04-16 00:39:53 +02:00
import de.ellpeck.prettypipes.items.ExtractionModuleItem;
import de.ellpeck.prettypipes.items.IModule;
2020-04-16 00:39:53 +02:00
import de.ellpeck.prettypipes.items.ModuleTier;
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-14 17:14:24 +02:00
import de.ellpeck.prettypipes.packets.PacketHandler;
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.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-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;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
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-04-14 01:38:48 +02:00
public static Item wrenchItem;
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-16 00:39:53 +02:00
IForgeRegistry<Item> registry = event.getRegistry();
registry.registerAll(
wrenchItem = new WrenchItem().setRegistryName("wrench")
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-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(
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());
int openModule = data.readInt();
IModule module = openModule < 0 ? null : (IModule) tile.modules.getStackInSlot(openModule).getItem();
return tile != null ? new PipeContainer(pipeContainer, windowId, inv.player, tile, module) : null;
2020-04-14 01:38:48 +02:00
}).setRegistryName("pipe")
);
}
2020-04-16 00:39:53 +02:00
private static Item[] createTieredModule(String name, Function<ModuleTier, Item> item) {
List<Item> items = new ArrayList<>();
for (ModuleTier tier : ModuleTier.values())
items.add(item.apply(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-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-14 17:14:24 +02:00
PacketHandler.setup();
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());
2020-04-14 17:14:24 +02:00
ClientRegistry.bindTileEntityRenderer(pipeTileEntity, PipeRenderer::new);
2020-04-14 01:38:48 +02:00
ScreenManager.registerFactory(pipeContainer, PipeGui::new);
2020-04-13 21:48:35 +02:00
}
}