Skip to content
Permalink
Browse files

Anvil is now complete!

  • Loading branch information...
alcatrazEscapee committed Feb 13, 2019
1 parent 27bffcf commit e6b3354ec9d7f0abf7efc63b1c092f3a333e837f
@@ -34,13 +34,13 @@
import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;

@Mod.EventBusSubscriber(modid = MOD_ID)
public class CommonEventHandler
public final class CommonEventHandler
{
/**
* Make leaves drop sticks
*/
@SubscribeEvent
public void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
public static void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
{
final EntityPlayer harvester = event.getHarvester();
final ItemStack heldItem = harvester == null ? ItemStack.EMPTY : harvester.getHeldItemMainhand();
@@ -69,7 +69,7 @@ public void onBlockHarvestDrops(BlockEvent.HarvestDropsEvent event)
* The event will fire AGAIN with the offhand and offhand stack.
*/
@SubscribeEvent
public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event)
public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event)
{
final World world = event.getWorld();
final BlockPos pos = event.getPos();
@@ -94,7 +94,7 @@ public void onRightClickBlock(PlayerInteractEvent.RightClickBlock event)
* Note: If you have an item that needs an active effect, use onItemRightClick(), or attach this via {@link IPlaceableItem.Impl}
*/
@SubscribeEvent
public void onRightClickItem(PlayerInteractEvent.RightClickItem event)
public static void onRightClickItem(PlayerInteractEvent.RightClickItem event)
{
final World world = event.getWorld();
final BlockPos pos = event.getPos();
@@ -116,7 +116,7 @@ public void onRightClickItem(PlayerInteractEvent.RightClickItem event)

//Used for IItemSize capability. You can either implement the interface or use the capability
@SubscribeEvent
public void attachItemCapabilities(AttachCapabilitiesEvent<ItemStack> e)
public static void attachItemCapabilities(AttachCapabilitiesEvent<ItemStack> e)
{
ItemStack stack = e.getObject();
// Skip items with existing capabilities
@@ -145,7 +145,7 @@ else if (item instanceof ItemBlock)
}

@SubscribeEvent
public void onPlayerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event)
public static void onPlayerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event)
{
if (event.player instanceof EntityPlayerMP)
{
@@ -155,7 +155,7 @@ public void onPlayerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event)
}

@SubscribeEvent
public void onPlayerCloneEvent(net.minecraftforge.event.entity.player.PlayerEvent.Clone event)
public static void onPlayerCloneEvent(net.minecraftforge.event.entity.player.PlayerEvent.Clone event)
{
if (event.getEntityPlayer() instanceof EntityPlayerMP)
{
@@ -165,7 +165,7 @@ public void onPlayerCloneEvent(net.minecraftforge.event.entity.player.PlayerEven
}

@SubscribeEvent
public void onContainerOpenEvent(PlayerContainerEvent.Open event)
public static void onContainerOpenEvent(PlayerContainerEvent.Open event)
{
if (event.getEntityPlayer() instanceof EntityPlayerMP)
{
@@ -6,7 +6,6 @@
package net.dries007.tfc;

import org.apache.logging.log4j.Logger;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.*;
import net.minecraftforge.fml.common.event.*;
@@ -119,8 +118,8 @@ public void preInit(FMLPreInitializationEvent event)
int id = 0;
// Received on server
network.registerMessage(new PacketKnappingUpdate.Handler(), PacketKnappingUpdate.class, ++id, Side.SERVER);
network.registerMessage(new PacketAnvilButton.Handler(), PacketAnvilButton.class, ++id, Side.SERVER);
network.registerMessage(new PacketAnvilRecipe.Handler(), PacketAnvilRecipe.class, ++id, Side.SERVER);
network.registerMessage(new PacketGuiButton.Handler(), PacketGuiButton.class, ++id, Side.SERVER);
// Received on client
network.registerMessage(new PacketAnvilRecipe.Handler(), PacketAnvilRecipe.class, ++id, Side.CLIENT);
network.registerMessage(new PacketChunkData.Handler(), PacketChunkData.class, ++id, Side.CLIENT);
@@ -136,8 +135,6 @@ public void preInit(FMLPreInitializationEvent event)
CapabilityItemHeat.preInit();
CapabilityForgeable.preInit();

MinecraftForge.EVENT_BUS.register(new CommonEventHandler());

if (event.getSide().isClient()) ClientEvents.preInit();
}

@@ -28,7 +28,7 @@
public GuiButtonAnvilPlan(TEAnvilTFC tile, int id, int guiLeft, int guiTop)
{
// Plan Button
super(id, guiLeft + 97, guiTop + 49, 18, 18, "");
super(id, guiLeft + 21, guiTop + 40, 18, 18, "");
this.tooltip = I18n.format("tfc.tooltip.anvil_plan");
this.tile = tile;
}
@@ -21,11 +21,11 @@

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.api.registries.TFCRegistries;
import net.dries007.tfc.client.TFCGuiHandler;
import net.dries007.tfc.client.button.GuiButtonAnvilPlanIcon;
import net.dries007.tfc.client.button.IButtonTooltip;
import net.dries007.tfc.network.PacketGuiButton;
import net.dries007.tfc.objects.te.TEAnvilTFC;
import net.dries007.tfc.util.NBTBuilder;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;
import static net.dries007.tfc.objects.te.TEAnvilTFC.SLOT_INPUT_1;
@@ -107,11 +107,8 @@ protected void actionPerformed(GuiButton button) throws IOException
{
// This fires when you select a plan in the Plan GUI
TerraFirmaCraft.getLog().info("Pressed the plan button");
TFCGuiHandler.openGui(tile.getWorld(), tile.getPos(), playerInv.player, TFCGuiHandler.Type.ANVIL);
ResourceLocation recipeName = ((GuiButtonAnvilPlanIcon) button).getRecipeName();
tile.setRecipeClient(TFCRegistries.ANVIL.getValue(recipeName));
//TerraFirmaCraft.getNetwork().sendToServer(new PacketAnvilRecipe(tile));
//TerraFirmaCraft.getLog().info("Set the recipe after pressing plan to {}", tile.getRecipe());
TerraFirmaCraft.getNetwork().sendToServer(new PacketGuiButton(button.id, new NBTBuilder().setString("recipe", recipeName.toString()).build()));
}
super.actionPerformed(button);
}
@@ -16,11 +16,10 @@

import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.client.TFCGuiHandler;
import net.dries007.tfc.client.button.GuiButtonAnvilPlan;
import net.dries007.tfc.client.button.GuiButtonAnvilStep;
import net.dries007.tfc.client.button.IButtonTooltip;
import net.dries007.tfc.network.PacketAnvilButton;
import net.dries007.tfc.network.PacketGuiButton;
import net.dries007.tfc.objects.te.TEAnvilTFC;
import net.dries007.tfc.util.forge.ForgeRule;
import net.dries007.tfc.util.forge.ForgeStep;
@@ -124,14 +123,7 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.id == BUTTON_ID_PLAN)
{
TFCGuiHandler.openGui(tile.getWorld(), tile.getPos(), playerInv.player, TFCGuiHandler.Type.ANVIL_PLAN);
}
else if (button.id >= BUTTON_ID_STEP_MIN && button.id <= BUTTON_ID_STEP_MAX)
{
TerraFirmaCraft.getNetwork().sendToServer(new PacketAnvilButton(button.id));
}
TerraFirmaCraft.getNetwork().sendToServer(new PacketGuiButton(button.id));
super.actionPerformed(button);
}

This file was deleted.

@@ -13,6 +13,7 @@
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;

import io.netty.buffer.ByteBuf;
import net.dries007.tfc.TerraFirmaCraft;
@@ -30,13 +31,17 @@
@SuppressWarnings("unused")
public PacketAnvilRecipe() {}

public PacketAnvilRecipe(TEAnvilTFC tile)
public PacketAnvilRecipe(TEAnvilTFC tile, AnvilRecipe recipe)
{
this.pos = tile.getPos();
AnvilRecipe recipe = tile.getRecipe();
this.recipe = recipe != null ? recipe.getRegistryName() : null;
}

public PacketAnvilRecipe(TEAnvilTFC tile)
{
this(tile, tile.getRecipe());
}

@Override
public void fromBytes(ByteBuf buffer)
{
@@ -73,13 +78,15 @@ public IMessage onMessage(PacketAnvilRecipe message, MessageContext ctx)
TEAnvilTFC te = Helpers.getTE(world, message.pos, TEAnvilTFC.class);
if (te != null)
{
TerraFirmaCraft.getLog().info("Side: " + ctx.side);
if (message.recipe == null)
{
te.setRecipe(null, false);
// The TE will send a reply if necessary, but only when this arrives on the server side
te.setRecipe(null, ctx.side == Side.SERVER);
}
else
{
te.setRecipe(TFCRegistries.ANVIL.getValue(message.recipe), false);
te.setRecipe(TFCRegistries.ANVIL.getValue(message.recipe), ctx.side == Side.SERVER);
}
}
});
@@ -0,0 +1,77 @@
package net.dries007.tfc.network;

import javax.annotation.Nullable;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import io.netty.buffer.ByteBuf;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.util.IButtonHandler;

/**
* This is a generic packet that sends a button notification to the players open container, which can delegate to the tile entity if needed
* See {@link net.dries007.tfc.client.gui.GuiAnvilTFC} for an example of its usage, and {@link net.dries007.tfc.objects.container.ContainerAnvilTFC} for an example of the message handling
*
* @author AlcatrazEscapee
*/
public class PacketGuiButton implements IMessage
{
private int buttonID;
private NBTTagCompound extraNBT;

@SuppressWarnings("unused")
public PacketGuiButton() {}

public PacketGuiButton(int buttonID, @Nullable NBTTagCompound extraNBT)
{
this.buttonID = buttonID;
this.extraNBT = extraNBT;
}

public PacketGuiButton(int buttonID)
{
this(buttonID, null);
}

@Override
public void fromBytes(ByteBuf buf)
{
buttonID = buf.readInt();
if (buf.readBoolean())
{
extraNBT = ByteBufUtils.readTag(buf);
}
}

@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(buttonID);
buf.writeBoolean(extraNBT != null);
if (extraNBT != null)
{
ByteBufUtils.writeTag(buf, extraNBT);
}
}

public static class Handler implements IMessageHandler<PacketGuiButton, IMessage>
{
@Override
public IMessage onMessage(PacketGuiButton message, MessageContext ctx)
{
EntityPlayer player = TerraFirmaCraft.getProxy().getPlayer(ctx);
TerraFirmaCraft.getProxy().getThreadListener(ctx).addScheduledTask(() -> {
if (player.openContainer instanceof IButtonHandler)
{
((IButtonHandler) player.openContainer).onButtonPress(message.buttonID, message.extraNBT);
}
});
return null;
}
}
}
@@ -5,19 +5,40 @@

package net.dries007.tfc.objects.container;

import javax.annotation.Nullable;

import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;

import net.dries007.tfc.api.recipes.AnvilRecipe;
import net.dries007.tfc.api.registries.TFCRegistries;
import net.dries007.tfc.client.TFCGuiHandler;
import net.dries007.tfc.objects.te.TEAnvilTFC;
import net.dries007.tfc.util.IButtonHandler;

public class ContainerAnvilPlan extends ContainerTE<TEAnvilTFC>
public class ContainerAnvilPlan extends ContainerTE<TEAnvilTFC> implements IButtonHandler
{
public ContainerAnvilPlan(InventoryPlayer playerInv, TEAnvilTFC tile)
{
super(playerInv, tile, true);
}

@Override
protected void addContainerSlots()
public void onButtonPress(int buttonID, @Nullable NBTTagCompound extraNBT)
{
if (extraNBT != null)
{
// Set the tile recipe
String recipeName = extraNBT.getString("recipe");
AnvilRecipe recipe = TFCRegistries.ANVIL.getValue(new ResourceLocation(recipeName));
tile.setRecipe(recipe, true);

// Switch to anvil GUI
TFCGuiHandler.openGui(player.world, tile.getPos(), player, TFCGuiHandler.Type.ANVIL);
}
}

@Override
protected void addContainerSlots() {}
}

0 comments on commit e6b3354

Please sign in to comment.
You can’t perform that action at this time.