[NMS] [1.12.2] Create an anvil inventory

Discussion in 'Spigot Plugin Development' started by Alex0589, Jun 26, 2018.

  1. Before you ask, I don't want to use the bukkit method but the nms one. So I created a CustomAnvil class:
    Code (Text):

    import net.minecraft.server.v1_12_R1.*;

    public class CustomAnvil extends ContainerAnvil {

        public CustomAnvil(EntityHuman human) {
            this(human.inventory, human.world, new BlockPosition(human.locX, human.locY, human.locZ), human);
        }

        public CustomAnvil(PlayerInventory playerinventory, World world, BlockPosition blockposition, EntityHuman entityhuman) {
            super(playerinventory, world, blockposition, entityhuman);
        }

        @Override
        public boolean c(EntityHuman human) {
            return true;
        }
    }
     
    and a TileEntity for the anvil:
    Code (Text):
    public class CustomAnvilTileEntity extends BlockAnvil.TileEntityContainerAnvil {

        public CustomAnvilTileEntity(World world, BlockPosition blockposition) {
            super(world, blockposition);
        }

        @Override
        public Container createContainer(PlayerInventory playerinventory, EntityHuman entityhuman) {
            return new CustomAnvil(entityhuman);
        }
    }
    I open the inventory in this way:
    Code (Text):
    import Anvil.CustomAnvilTileEntity;
    import net.minecraft.server.v1_12_R1.BlockPosition;
    import net.minecraft.server.v1_12_R1.EntityHuman;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class PlayerInteracts implements Listener {
        @EventHandler
        public void onClick(PlayerInteractEvent event){
            System.out.println("DEBUG: OK!");
            if(event.getClickedBlock() == null){
                return;
            }
            if(event.getAction() != Action.RIGHT_CLICK_BLOCK){
                return;
            }

            if(event.getClickedBlock().getType() != Material.ANVIL){
                return;
            }
            event.setCancelled(true);
            Player bukkitPlayer = event.getPlayer();
            CraftPlayer player = (CraftPlayer) bukkitPlayer;
            EntityHuman human = player.getHandle();

            CustomAnvilTileEntity anvil = new CustomAnvilTileEntity(human.world, new BlockPosition(human.locX, human.locY, human.locZ));
            human.openTileEntity(anvil);

            System.out.println("DEBUG: OK");
        }
    }
     
    Well, it doesn't work: i see the inventory for a millisecond but then it disappears
     
  2. Still need help. Plz