Skip to content

Commit

Permalink
Cleaned up gui handling
Browse files Browse the repository at this point in the history
  • Loading branch information
T145 committed Jul 15, 2019
1 parent 552633f commit 44347eb
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 139 deletions.
112 changes: 95 additions & 17 deletions src/main/java/t145/metaltransport/core/MetalTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

import T145.tbone.core.TBone;
import T145.tbone.dispenser.BehaviorDispenseMinecart;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBeacon;
import net.minecraft.block.BlockEnchantmentTable;
import net.minecraft.block.BlockWorkbench;
import net.minecraft.client.gui.GuiEnchantment;
import net.minecraft.client.gui.inventory.GuiBeacon;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.item.EntityMinecartEmpty;
Expand All @@ -14,6 +21,10 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerBeacon;
import net.minecraft.inventory.ContainerEnchantment;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
Expand All @@ -24,6 +35,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.common.capabilities.Capability;
Expand All @@ -45,13 +57,16 @@
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.EntityEntry;
import net.minecraftforge.fml.common.registry.EntityEntryBuilder;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.registries.DataSerializerEntry;
import net.minecraftforge.registries.IForgeRegistry;
import shadows.fastbench.gui.ContainerFastBench;
import shadows.fastbench.gui.GuiFastBench;
import t145.metaltransport.api.caps.CapabilityCartType;
import t145.metaltransport.api.consts.CartType;
import t145.metaltransport.api.consts.ItemCartType;
Expand All @@ -65,16 +80,17 @@
import t145.metaltransport.client.render.entities.RenderTntCart;
import t145.metaltransport.entities.EntityFurnaceCart;
import t145.metaltransport.entities.EntityMetalCart;
import t145.metaltransport.entities.profiles.BeaconProfile;
import t145.metaltransport.entities.profiles.BeaconProfile.ProfileFactoryBeacon;
import t145.metaltransport.entities.profiles.CraftingTableProfile.CraftingTableProfileFactory;
import t145.metaltransport.entities.profiles.CraftingTableProfile.ProfileFactoryCraftingTable;
import t145.metaltransport.entities.profiles.EnchantingTableProfile.ProfileFactoryEnchantingTable;
import t145.metaltransport.entities.profiles.EnderChestProfile.ProfileFactoryEnderChest;
import t145.metaltransport.entities.profiles.JukeboxProfile.JukeboxProfileFactory;
import t145.metaltransport.entities.profiles.JukeboxProfile.ProfileFactoryJukebox;
import t145.metaltransport.items.ItemCart;

@Mod(modid = RegistryMT.ID, name = RegistryMT.NAME, version = MetalTransport.VERSION, updateJSON = MetalTransport.UPDATE_JSON, dependencies = "required-after:tbone;after:metalchests")
@EventBusSubscriber
public class MetalTransport {
public class MetalTransport implements IGuiHandler {

public static final String VERSION = "@VERSION@";
public static final String UPDATE_JSON = "https://raw.githubusercontent.com/T145/metaltransport/master/update.json";
Expand All @@ -90,6 +106,77 @@ public static boolean isDeobfuscated() {
@Instance(RegistryMT.ID)
public static MetalTransport instance;

@Override
public Container getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
EntityMetalCart cart = (EntityMetalCart) world.getEntityByID(ID);

if (cart != null && cart.getProfile().isPresent()) {
Block block = cart.getDisplayBlock();

if (block instanceof BlockWorkbench) {
return new ContainerFastBench(player, world, cart.getPosition()) {

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}

if (block instanceof BlockEnchantmentTable) {
return new ContainerEnchantment(player.inventory, world, cart.getPosition()) {

@Override
public void onCraftMatrixChanged(IInventory inventory) {
super.onCraftMatrixChanged(inventory);
// TODO: Tweak this to get influenced by cart contents that increase enchant power
}

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}

if (block instanceof BlockBeacon) {
return new ContainerBeacon(player.inventory, (BeaconProfile) cart.getProfile().get()) {

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}
}

return null;
}

@SideOnly(Side.CLIENT)
@Override
public GuiContainer getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
EntityMetalCart cart = (EntityMetalCart) world.getEntityByID(ID);

if (cart != null && cart.getProfile().isPresent()) {
Block block = cart.getDisplayBlock();

if (block instanceof BlockWorkbench) {
return new GuiFastBench(player.inventory, world, cart.getPosition());
}

if (block instanceof BlockEnchantmentTable) {
return new GuiEnchantment(player.inventory, world, (IWorldNameable) cart.getProfile().get());
}

if (block instanceof BlockBeacon) {
return new GuiBeacon(player.inventory, (BeaconProfile) cart.getProfile().get());
}
}

return null;
}

@EventHandler
public void metaltransport$preInit(final FMLPreInitializationEvent event) {
ModMetadata meta = event.getModMetadata();
Expand All @@ -107,6 +194,7 @@ public static boolean isDeobfuscated() {

@EventHandler
public void metaltransport$init(final FMLInitializationEvent event) {
NetworkRegistry.INSTANCE.registerGuiHandler(instance, this);
CapabilityManager.INSTANCE.register(CapabilityCartType.class, new Capability.IStorage<CapabilityCartType>() {

@Nullable
Expand All @@ -125,20 +213,10 @@ public void readNBT(Capability<CapabilityCartType> capability, CapabilityCartTyp
public void metaltransport$postInit(final FMLPostInitializationEvent event) {
BehaviorDispenseMinecart.register(ItemsMT.METAL_MINECART, ItemCart.DISPENSER_BEHAVIOR);
ProfileRegistry.register(Blocks.ENDER_CHEST, new ProfileFactoryEnderChest());

ProfileFactoryEnchantingTable enchantingFactory = new ProfileFactoryEnchantingTable();
ProfileRegistry.register(Blocks.ENCHANTING_TABLE, enchantingFactory);
NetworkRegistry.INSTANCE.registerGuiHandler(instance, enchantingFactory);

CraftingTableProfileFactory craftingFactory = new CraftingTableProfileFactory();
ProfileRegistry.register(Blocks.CRAFTING_TABLE, craftingFactory);
NetworkRegistry.INSTANCE.registerGuiHandler(instance, craftingFactory);

ProfileFactoryBeacon beaconFactory = new ProfileFactoryBeacon();
ProfileRegistry.register(Blocks.BEACON, beaconFactory);
NetworkRegistry.INSTANCE.registerGuiHandler(instance, beaconFactory);

ProfileRegistry.register(Blocks.JUKEBOX, new JukeboxProfileFactory());
ProfileRegistry.register(Blocks.ENCHANTING_TABLE, new ProfileFactoryEnchantingTable());
ProfileRegistry.register(Blocks.CRAFTING_TABLE, new ProfileFactoryCraftingTable());
ProfileRegistry.register(Blocks.BEACON, new ProfileFactoryBeacon());
ProfileRegistry.register(Blocks.JUKEBOX, new ProfileFactoryJukebox());
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,30 @@
package t145.metaltransport.entities.profiles;

import me.paulf.rbeacons.server.block.TileResponsiveBeacon;
import net.minecraft.client.gui.inventory.GuiBeacon;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerBeacon;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatList;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import t145.metaltransport.api.consts.RegistryMT;
import t145.metaltransport.api.profiles.IProfileFactory;
import t145.metaltransport.api.profiles.IUniversalProfile;
import t145.metaltransport.entities.EntityMetalCart;

public class BeaconProfile extends TileResponsiveBeacon implements IUniversalProfile {

public static class ProfileFactoryBeacon implements IProfileFactory, IGuiHandler {
public static class ProfileFactoryBeacon implements IProfileFactory {

@Override
public BeaconProfile create(EntityMinecart cart) {
return new BeaconProfile(cart);
}

@Override
public ContainerBeacon getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
EntityMetalCart cart = (EntityMetalCart) entity;
return new ContainerBeacon(player.inventory, (BeaconProfile) cart.getProfile().get()) {

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}

return null;
}

@SideOnly(Side.CLIENT)
@Override
public GuiBeacon getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
EntityMetalCart cart = (EntityMetalCart) entity;
return new GuiBeacon(player.inventory, (BeaconProfile) cart.getProfile().get());
}

return null;
}
}

private final EntityMinecart cart;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,23 @@
package t145.metaltransport.entities.profiles;

import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.stats.StatList;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import shadows.fastbench.gui.ContainerFastBench;
import shadows.fastbench.gui.GuiFastBench;
import t145.metaltransport.api.consts.RegistryMT;
import t145.metaltransport.api.profiles.IProfileFactory;
import t145.metaltransport.api.profiles.IServerProfile;
import t145.metaltransport.entities.EntityMetalCart;

public class CraftingTableProfile implements IServerProfile {

public static class CraftingTableProfileFactory implements IProfileFactory, IGuiHandler {
public static class ProfileFactoryCraftingTable implements IProfileFactory {

@Override
public CraftingTableProfile create(EntityMinecart cart) {
return new CraftingTableProfile(cart);
}

@Override
public ContainerFastBench getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
EntityMetalCart cart = (EntityMetalCart) entity;
return new ContainerFastBench(player, world, cart.getPosition()) {

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}

return null;
}

@SideOnly(Side.CLIENT)
@Override
public GuiFastBench getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
return new GuiFastBench(player.inventory, world, ((EntityMetalCart) entity).getPosition());
}

return null;
}
}

private final EntityMinecart cart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiEnchantment;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerEnchantment;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityEnchantmentTable;
Expand All @@ -23,7 +19,6 @@
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.fml.common.network.IGuiHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import t145.metaltransport.api.consts.RegistryMT;
Expand All @@ -33,49 +28,12 @@

public class EnchantingTableProfile extends TileEntityEnchantmentTable implements IUniversalProfile, IWorldNameable {

public static class ProfileFactoryEnchantingTable implements IProfileFactory, IGuiHandler {
public static class ProfileFactoryEnchantingTable implements IProfileFactory {

@Override
public EnchantingTableProfile create(EntityMinecart cart) {
return new EnchantingTableProfile(cart);
}

@Override
public ContainerEnchantment getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
EntityMetalCart cart = (EntityMetalCart) entity;
return new ContainerEnchantment(player.inventory, world, cart.getPosition()) {

@Override
public void onCraftMatrixChanged(IInventory inventory) {
super.onCraftMatrixChanged(inventory);
// TODO: Tweak this to get influenced by cart contents that increase enchant power
}

@Override
public boolean canInteractWith(EntityPlayer player) {
return cart.isEntityAlive() && player.getDistanceSq(cart.posX + 0.5D, cart.posY + 0.5D, cart.posZ + 0.5D) <= 64.0D;
}
};
}

return null;
}

@SideOnly(Side.CLIENT)
@Override
public GuiEnchantment getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
Entity entity = world.getEntityByID(ID);

if (entity instanceof EntityMetalCart) {
EntityMetalCart cart = (EntityMetalCart) entity;
return new GuiEnchantment(player.inventory, world, (IWorldNameable) cart.getProfile().get());
}

return null;
}
}

private final EntityMinecart cart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class JukeboxProfile implements IServerProfile {

public static class JukeboxProfileFactory implements IProfileFactory {
public static class ProfileFactoryJukebox implements IProfileFactory {

@Override
public JukeboxProfile create(EntityMinecart cart) {
Expand Down
Loading

0 comments on commit 44347eb

Please sign in to comment.