Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2] Prevent all Inventory interaction server-side
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
xorinzor

[1.12.2] Prevent all Inventory interaction server-side

By xorinzor, January 15, 2018 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 15, 2018 (edited)

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 January 15, 2018 by xorinzor
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15996 posts
Posted January 15, 2018

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

  • Quote

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.

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 15, 2018
7 hours ago, Draco18s said:

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

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

  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 15, 2018 (edited)

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 January 15, 2018 by xorinzor
  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15996 posts
Posted January 15, 2018
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.

  • Quote

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.

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 16, 2018
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 :/ 
 

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15996 posts
Posted January 16, 2018

Here's an example (of supplying it) in a TE.

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/ores/entities/TileEntityMillstone.java#L176-L178

  • Quote

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.

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • brok4d
      OBJ MODELS

      By brok4d · Posted 17 minutes ago

      Hello, this mod is the source, you have to get boredhttps://gitlab.com/Lycanite/LycanitesMobs
    • JayNeedsHelp
      Logger not working

      By JayNeedsHelp · Posted 46 minutes ago

      So I'm currently creating a forge mod and I'm having an issue where the console stops logging after some errors. It seems to be connected to the access transformers that I'm using as before I added at's my console was working fine.   Here is my at file:  public-f net.minecraft.client.Minecraft session public net.minecraft.client.Minecraft timer public net.minecraft.client.gui.GuiScreen buttonList public net.minecraft.util.Timer tickLength public net.minecraft.network.play.client.CPacketPlayer onGround public net.minecraft.network.play.server.SPacketEntityVelocity motionX public net.minecraft.network.play.server.SPacketEntityVelocity motionY public net.minecraft.network.play.server.SPacketEntityVelocity motionZ public net.minecraft.network.play.server.SPacketExplosion motionX public net.minecraft.network.play.server.SPacketExplosion motionY public net.minecraft.network.play.server.SPacketExplosion motionZ public net.minecraft.client.renderer.entity.RenderManager renderPosX public net.minecraft.client.renderer.entity.RenderManager renderPosY public net.minecraft.client.renderer.entity.RenderManager renderPosZ   Any help is greatly appreciated thank you!
    • cadbane86140
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140 · Posted 2 hours ago

      Hello There! Today we are back on Hunger Games after a little break but we are finally back! In this episode we are on the good ol' map Survival Games 4 and it ACTUALLY went well for once. Also we have so many great battles on rooftops, small rooms and just out in the open! We also use shears to fight at one point and that was pretty crazy! There are so many hilarious moments in this episode that I know you guys are gonna love! I hope you all enjoy this video and if you did don't forget to like and sub for more Hunger Games in the future!  
    • Sad Whale
      Game crashes whenever I try to increase the RAM

      By Sad Whale · Posted 2 hours ago

      latest.log
    • diesieben07
      Game crashes whenever I try to increase the RAM

      By diesieben07 · Posted 2 hours ago

      In the logs folder of your game directory.
  • Topics

    • Milk_Shak3s
      1
      OBJ MODELS

      By Milk_Shak3s
      Started 15 hours ago

    • JayNeedsHelp
      0
      Logger not working

      By JayNeedsHelp
      Started 47 minutes ago

    • cadbane86140
      0
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140
      Started 2 hours ago

    • Sad Whale
      6
      Game crashes whenever I try to increase the RAM

      By Sad Whale
      Started 3 hours ago

    • Unusualty
      0
      GUI'S and player editing

      By Unusualty
      Started 2 hours ago

  • Who's Online (See full list)

    • Sad Whale
    • PyRoTheLifeLess
    • Woosh
    • HexaGoat49
    • rhinitis
    • JayNeedsHelp
    • brok4d
    • Chumbanotz
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2] Prevent all Inventory interaction server-side
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community