Jump to content

[1.12.2] Prevent all Inventory interaction server-side


xorinzor

Recommended Posts

Should probably start with that I've read multiple posts and checked various github repos / tutorials about this, but most of them seem under the impression that a client-side plugin is involved too, which isn't for me the case.
Since it's server-side, there simply has to be a way to prevent a user from grabbing the item, as the server stores the data and not the client (else you could just spawn in items with a hacked client too).

Currently I have a custom IInventory with this method:

@Override
public ItemStack removeStackFromSlot(int i)
{
	return ItemStack.EMPTY;
}

 

Assign custom slots, albeit a bit of a hackish way, since I cannot find the "addSlotToContainer" method anywhere, via:
 

List<Slot> c = fakePlayer.get().inventoryContainer.inventorySlots;
  
//this is inside a for-loop with variable: i
c.set(i, new MyCustomSlot(inventory, i, 0, 0));
inventory.setInventorySlotContents(i, itemStack);

 

Where the MyCustomSlot class contains:
 

@Override
public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack)
{
    return ItemStack.EMPTY;
}

/**
 * Return whether this slot's stack can be taken from this slot.
 */
@Override
public boolean canTakeStack(EntityPlayer playerIn)
{
    return false;
}

/**
 * Returns if this slot contains a stack.
 */
@Override
public boolean getHasStack()
{
    return false;
}
    
/**
 * Helper method to put a stack in the slot.
 */
@Override
public void putStack(ItemStack stack)
{
    //DO nothing.
}

 

This prevents the click events so far, but shift-click still works (as well as putting items into the inventory, which I dont want to happen either).

And this is the point where I'm stuck, since I have no idea how I can make my fakeplayer use my Custom Container to override the "transferStackInSlot" method which would prevent the shift-click.

It's really frustrating to have everything working, but that a simple InventoryInteractEvent or something similar just isn't implemented, so I'm really hoping someone can help me on the right track here.
Feel like it's worth noting that I don't mind a hackish way if that's required. It's not a mod thats going to be published, it's for private use only.

Edited by xorinzor
Link to comment
Share on other sites

You will probably need a custom ItemStackHandler class that prevents the extraction.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

After multiple different attempts I haven't really gotten a single step further unfortunately.

I did change my code where I extend the FakePlayerFactory, FakePlayer and it's PlayerInventory and InventoryContainer (with subsequent Slot's) classes, but overriding all methods relating to getting, setting and transferring ItemStacks didn't seem to prevent the shift+click either. Regular grabbing still gets prevented though, inserting items unfortunately is still possible too.

EDIT: changed the title of the topic too, as I feel this may better reflect the goal.

EDIT 2: The classes that I currently have

CustomFakePlayer and CustomFakePlayerFactory

Spoiler

public class CustomFakePlayer extends FakePlayer {

	public CustomFakePlayer(WorldServer world, GameProfile name) {
		super(world, name);
		
		this.inventory = new CustomInventory(this);
		this.inventoryContainer = new CustomContainerPlayer(this.inventory, true, this);
	}

}

public class CustomFakePlayerFactory extends FakePlayerFactory {
    // Map of all active fake player usernames to their entities
    private static Map<GameProfile, FakePlayer> fakePlayers = Maps.newHashMap();

    /**
     * Get a fake player with a given username,
     * Mods should either hold weak references to the return value, or listen for a
     * WorldEvent.Unload and kill all references to prevent worlds staying in memory.
     */
    public static FakePlayer get(WorldServer world, GameProfile username)
    {
        if (!fakePlayers.containsKey(username))
        {
            FakePlayer fakePlayer = new CustomFakePlayer(world, username);
            fakePlayers.put(username, fakePlayer);
        }

        return fakePlayers.get(username);
    }

    public static void unloadWorld(WorldServer world)
    {
        fakePlayers.entrySet().removeIf(entry -> entry.getValue().world == world);
    }
}

 

 

CustomInventory

Spoiler

public class CustomInventory extends InventoryPlayer
{
	public boolean available = true;
	
	public CustomInventory(EntityPlayerMP ep)
	{
		super(ep);
	}

	@Override
	public int getSizeInventory()
    {
		return 9 * 4;
    }
	
	public String getName()
    {
        return "My custom inventory";
    }

	@Override
	public void fillStackedContents(RecipeItemHelper helper, boolean p_194016_2_)
    {
		return;
    }
	
	@Override
	public boolean add(int p_191971_1_, final ItemStack p_191971_2_)
    {
		return false;
    }
	
    /**
     * Returns true if this thing is named
     */
    public boolean hasCustomName()
    {
        return false;
    }
    
    @Override
    public boolean isUsableByPlayer(EntityPlayer player)
    {
    	return available;
    }
    
    public void disableInventory() {
    	this.available = false;
    }
    
	/**
     * Returns the item stack currently held by the player.
     */
	@Override
    public ItemStack getCurrentItem()
    {
        return ItemStack.EMPTY;
    }
	
	@Override
	public int getFirstEmptyStack()
    {
		return -1;
    }
	
	@Override
	public void pickItem(int index)
    {
		return;
    }
	
	@Override
	public int clearMatchingItems(@Nullable Item itemIn, int metadataIn, int removeCount, @Nullable NBTTagCompound itemNBT)
    {
		return 0;
    }
	
	@Override
	public int storeItemStack(ItemStack itemStackIn)
    {
		return -1;
    }
	
	@Override
	public boolean addItemStackToInventory(ItemStack itemStackIn)
    {
		return false;
    }
	
	@Override
	public ItemStack decrStackSize(int index, int count)
    {
		return ItemStack.EMPTY;
    }
	
	@Override
	public void deleteStack(ItemStack stack)
    {
		return;
    }
	
	@Override
	public ItemStack removeStackFromSlot(int index)
    {
		return ItemStack.EMPTY;
    }

	@Override
	public void dropAllItems()
    {
		return;
    }
	
	/**
     * Set the stack helds by mouse, used in GUI/Container
     */
	@Override
    public void setItemStack(ItemStack itemStackIn)
    {
        return;
    }

    /**
     * Stack helds by mouse, used in GUI and Containers
     */
    @Override
    public ItemStack getItemStack()
    {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean hasItemStack(ItemStack itemStackIn)
    {
    	return false;
    }
    
    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack)
    {
    	return false;
    }
    
    @Override
    public void copyInventory(InventoryPlayer playerInventory)
    {
    	return;
    }
    
}

 

 

CustomContainerPlayer
 

Spoiler

public class CustomContainerPlayer extends ContainerPlayer {
    /** The crafting matrix inventory. */
    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
    public InventoryCraftResult craftResult = new InventoryCraftResult();
    /** Determines if inventory manipulation should be handled. */
    public boolean isLocalWorld;
    
    public CustomContainerPlayer(InventoryPlayer playerInventory, boolean localWorld, EntityPlayer player) {
        super(playerInventory, localWorld, player);
        
        this.isLocalWorld = localWorld;
        this.inventorySlots.clear();

        this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 154, 28));
        
        for (int i = 0; i < 2; ++i)
        {
            for (int j = 0; j < 2; ++j)
            {
                this.addSlotToContainer(new CustomSlot(this.craftMatrix, j + i * 2, 98 + j * 18, 18 + i * 18));
            }
        }

        for (int k = 0; k < 4; ++k)
        {
            this.addSlotToContainer(new CustomSlot(playerInventory, 36 + (3 - k), 8, 8 + k * 18));
        }

        for (int l = 0; l < 3; ++l)
        {
            for (int j1 = 0; j1 < 9; ++j1)
            {
                this.addSlotToContainer(new CustomSlot(playerInventory, j1 + (l + 1) * 9, 8 + j1 * 18, 84 + l * 18));
            }
        }

        for (int i1 = 0; i1 < 9; ++i1)
        {
            this.addSlotToContainer(new CustomSlot(playerInventory, i1, 8 + i1 * 18, 142));
        }

        this.addSlotToContainer(new CustomSlot(playerInventory, 40, 77, 62));
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean canMergeSlot(ItemStack stack, Slot slotIn) {
        return false;
    }
    
    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, EntityPlayer player)
    {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean getCanCraft(EntityPlayer player)
    {
        return false;
    }
    
    @Override
    protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection)
    {
        return false;
    }
    
    @Override
    public boolean canDragIntoSlot(Slot slotIn)
    {
        return false;
    }
}

 


CustomSlot
 

Spoiler

public class CustomSlot extends Slot {

	public CustomSlot(IInventory inventoryIn, int index, int xPosition, int yPosition) {
		super(inventoryIn, index, xPosition, yPosition);	
	}
	
	@Override
	public void onSlotChange(ItemStack p_75220_1_, ItemStack p_75220_2_){
		return;
    }
	
	@Override
	public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack)
    {
        return ItemStack.EMPTY;
    }
	
	@Override
	public boolean isItemValid(ItemStack stack) {
		return false;
    }

	/**
     * Return whether this slot's stack can be taken from this slot.
     */
	@Override
    public boolean canTakeStack(EntityPlayer playerIn)
    {
        return false;
    }
	
	/**
     * Returns if this slot contains a stack.
     */
	@Override
    public boolean getHasStack()
    {
        return true;
    }
        
    /**
     * Helper method to put a stack in the slot.
     */
    @Override
    public void putStack(ItemStack stack)
    {
        //DO nothing.
    }
	
}

 

 

Edited by xorinzor
Link to comment
Share on other sites

5 hours ago, xorinzor said:

Interesting, haven't seen any of the resources I've read use such a class. I'll look into that. Thanks.

It's a Capability. Vanilla doesn't use it, but there are wrappers around all of the IInventory implementations that return an ItemStackHandler.

Not sure how you'd go about making it do its thing in this case, though.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

7 hours ago, Draco18s said:

It's a Capability. Vanilla doesn't use it, but there are wrappers around all of the IInventory implementations that return an ItemStackHandler.

Not sure how you'd go about making it do its thing in this case, though.

I havent found any references to the ItemStackHandler yet though, and the examples and tutorials about Capabilities that I've seen on Capabilities are really confusing :/ 
 

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.