Furnace Events

Discussion in 'Plugin Development' started by Samkio, Apr 10, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Samkio

    Hey everyone first off thanks for taking the time to read the post. :)
    Okay the story so far...
    To keep it short basically i want to create a way of identifying when an object is placed or taken from a furnace.
    This was done by first creating a thread which happens when a player interacts with the furnace. This thread is then closed when the user exits the furnace GUI.

    The thread checks the ItemStacks in each of the 3 slots of the furnaces and the ItemStack in the players hand:


    Code:
    ItemStack[] Current = {tileEntity.c_(0),tileEntity.c_(1),tileEntity.c_(2), inventory.j()};
            smith.previousResult.put(id, Current);


    Then when the code is re-run it grabs the previous state of the furnace:

    Code:
        if (smith.previousResult.containsKey(id)) {
                    Prev = smith.previousResult.get(id);
                    smith.previousResult.remove(id);
                    pIngredient = Prev[0];
                    pFuel = Prev[1];
                    pResult = Prev[2];
                    pHand = Prev[3];
                }
    Getting the previous result comes before setting the current of course.
    Thus by doing the following code:
    Code:
    if(pResult == inventory.j() && pResult != null){
                craftPlayer.sendMessage("You took something from the result box.");
            }
    This code above works. So basically when the whole stack is picked up from the result box and moved the the players hand.

    Now thats great and all but what about if i don't pick up the entire stack?
    Thus i attempted this code:
    Code:
    if(result != null && pResult != null){
            if(result.count != pResult.count){
                craftPlayer.sendMessage("You took something but not all of it");
            }
            }
            
    So the above code will see if there was a change in count of the stack (be it up or down.)
    This however does not work. This is because the result and pResult are the same for some reason.
    This picture depicts it well:
    [​IMG]

    As you can see the hand has the implemented time lag. (pH = previous hand, cH = current hand).
    As it shows it from going from null to an itemstack of 1 sand.
    The Result however does not have this time lag. It goes from 2xtile of sand down to 1xtile of sand without the previous. This has had my head hurting all weekend i can't see anything wrong or why it is doing this. :eek:

    If anyone could help it would be greatly appreciated.
    Regards
    Samkio
     
  2. Offline

    Carnes

    Can i see more? Or enough to get a running copy to look at?
     
  3. Offline

    Samkio

    @Carnes sure. I'll tidy it up a bit 2secs.

    Code:
    package me.samkio.levelcraft.SamToolbox;
    
    import java.lang.reflect.Field;
    
    import me.samkio.levelcraft.Levelcraft;
    import me.samkio.levelcraft.Settings;
    import me.samkio.levelcraft.Functions.PlayerFunctions;
    import me.samkio.levelcraft.Skills.Forge;
    import net.minecraft.server.ContainerFurnace;
    import net.minecraft.server.EntityPlayer;
    import net.minecraft.server.InventoryPlayer;
    import net.minecraft.server.ItemStack;
    import net.minecraft.server.TileEntityFurnace;
    
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    
    public class FurnaceWorkThread implements Runnable {
        private CraftPlayer craftPlayer;
        private EntityPlayer entityPlayer;
        private Levelcraft plugin;
        private int id;
        private InventoryPlayer inventory;
        private Forge smith;
    
        public FurnaceWorkThread(Player p, Levelcraft plugin) {
            this.craftPlayer = (CraftPlayer) p;
            this.entityPlayer = craftPlayer.getHandle();
            this.plugin = plugin;
            this.inventory = entityPlayer.inventory; // get its inventory.
    
        }
    
        public void addID(int id) {
            this.id = id;
        }
     
        @SuppressWarnings({ "unused", "static-access" })
        @Override
        public void run() {
    
            if (entityPlayer == null) {
                kill();
                return;
            }
            ContainerFurnace furnace = null;
            try {
                furnace = (ContainerFurnace) entityPlayer.activeContainer;
            } catch (Exception ex) {
                kill();
                return;
            }
            Field privateTileEntity;
            try {
                privateTileEntity = ContainerFurnace.class.getDeclaredField("a");
            } catch (Exception ex) {
    
                return;
            }
            privateTileEntity.setAccessible(true);
            TileEntityFurnace tileEntity;
    
            try {
                tileEntity = (TileEntityFurnace) privateTileEntity.get(furnace);
            } catch (Exception ex) {
                return;
            }
            ItemStack result = tileEntity.c_(2);
            ItemStack stack = tileEntity.c_(0);
            ItemStack[] Prev = null;
            ItemStack pIngredient = null;
            ItemStack pFuel = null;
            ItemStack pResult = null;
            ItemStack pHand = null;
            if (smith.previousResult.containsKey(id)) {
                    Prev = smith.previousResult.get(id);
                    smith.previousResult.remove(id);
                    pIngredient = Prev[0];
                    pFuel = Prev[1];
                    pResult = Prev[2];
                    pHand = Prev[3];
                }
            if(pResult != result) craftPlayer.sendMessage("ResultChange");
            if(pFuel != tileEntity.c_(1)) craftPlayer.sendMessage("FuelChange");
            if(pIngredient != tileEntity.c_(0)) craftPlayer.sendMessage("IngChange");
            if(pHand != inventory.j()) craftPlayer.sendMessage("handChange");
            ItemStack[] Current = {tileEntity.c_(0),tileEntity.c_(1),tileEntity.c_(2), inventory.j()};
            smith.previousResult.put(id, Current);
            if(pResult == inventory.j() && pResult != null){
                craftPlayer.sendMessage("You took something from the result box,");
            }
            if(result != null && pResult != null){
    
            if(resul.count != pResult.count){
                craftPlayer.sendMessage("You took something fromt he result box but not all of it.");
            }
            }
    
    
            if (!craftPlayer.isOnline())
                kill();
        }
    
        @SuppressWarnings("static-access")
        public void kill() {
            plugin.getServer().getScheduler().cancelTask(id);
            int index = smith.tasks.indexOf(id);
            smith.previousResult.remove(id);
            smith.previousResult2.remove(id);
            if (index != -1)
                smith.tasks.remove(smith.tasks.indexOf(id));
    
        }
    }
    
    Thanks teh FurnaceWorkThread (Modified from Fullwalls cookbook. thanks fullwall :).)
    This is a repeating task every 2 that is started when a furnace is rightclicked.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  4. Offline

    Samkio

    Hmm i have still had no luck.
    If i ever get this working, or someone else it will be a very useful bit of code for dealing with furnaces.
    Shame i can't get it to work. It should work though :(
     
  5. Offline

    Samkio

    Has anyone found a work around?
     
Thread Status:
Not open for further replies.

Share This Page