Jump to content

xorinzor

Members
  • Posts

    15
  • Joined

  • Last visited

Posts posted by xorinzor

  1. 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 :/ 
     

  2. 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.
        }
    	
    }

     

     

  3. 1 minute ago, American2050 said:

    There is this method you can use also. Just check what int it returns and if it's on the range of inventory slots you want (Because I don't know if it would also return armor slots as empty slots)

    player.inventory.getFirstEmptyStack()

     

    Depending on what number you get, you will know that the player has a free slot.

    Awesome. That's exactly the kind of method I was looking for.

  4. 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.

  5. 7 minutes ago, diesieben07 said:

    You are not using the method World::playSound in the way it is designed. Check out the documentation on playing sounds.

    Oh, right, I did read the documentation, but misinterpreted the client/server side way. Should've figured since servers can't play sounds. derp.

     

    9 minutes ago, Kokkie said:

    Do you check if you're on the client or server side?

    Also, I don't think you should create a new SoundEvent, rather use an existing one

    I don't check client or server side, since my mod is just a customization for the experience on my server.
    Good feedback on the SoundEvent though, that definitely helps!

  6. I'm trying to play a sound-effect for a player, via server-side code. However, none of these seem to trigger the sound effect, leaving me wondering if vanilla sounds use a different notation:
     

    ResourceLocation location = new ResourceLocation("minecraft:block.note.hat");
    
    ResourceLocation location = new ResourceLocation("minecraft", "block.note.hat");
    
    ResourceLocation location = new ResourceLocation("block.note.hat");


    and using them with:

    player.world.playSound(player, player.getPosition(), new SoundEvent(location), SoundCategory.MASTER, 1, 1);


    What's going wrong here?

  7. 5 minutes ago, diesieben07 said:

    You don't need an iterator. There are several nice ways to do it using streams if you want to :D

    haha oh boy, I'd rather not :P

    This seems to work, so I'll just go with it.

     

    public void someMethod() {
    
    	checkFreeSlot: {
    		for(int i=0; i < 36; i++) {
    			if(player.inventory.getStackInSlot(i).equals(ItemStack.EMPTY)) {
    				break checkFreeSlot;
    			}
    		}
    		
    		//Failed check	
    		return;
    	}
    
    	//Check passed, do something.
    }

     

  8. 1 minute ago, diesieben07 said:

    You will need to write a for loop and check for empty stacks. But most likely you don't want to actually find an empty stack, you want to check if there is room for some other stack, right?

     

    Yea, all it needs to do is just check for a empty spot. Was hoping a iterator wouldn't be necessary, but I guess not haha.

  9. So, maybe I'm just trying to cut corners here, but none of these IF-statements seem to be returning true, despite the inventory definitely having empty slots.
     

    if(player.inventory.hasItemStack(ItemStack.EMPTY)) {
    	//Do something
    }
    
    if(player.inventory.hasItemStack(new ItemStack(Items.AIR))) {
    	//Do something
    }
    
    if(player.inventory.hasItemStack(new ItemStack(Blocks.AIR))) {
    	//Do something
    }


    Am I really going to have to add the code to iterate over all slots in the inventory? or is there a much simpler way that I'm just not seeing here? (something along the lines of, less = more. Especially when it comes to readability).

    EDIT: Probably worth noting, this is server-side code.

  10. Hi,

    I've been trying to make a server-side mod to open up a regular chest inventory screen on a client and display some items, but for some reason the screen either flashes very briefly, or just doesnt open at all.
    I thought maybe it was something to do with setting Inventory slot contents, but after commenting out that code, the problem persisted.

    This is the class that opens up the inventory as well as updates it (haven't gotten to that part yet though.)
     

    public class RandOMat {
    	
    	private final EntityPlayerMP target;
    	private final EntiyPlayerMP fakePlayer;
    	
    	private int stepCounter = 0;
    	private int counterMax = 5;
    	
    	public RandOMat(OxideForgeVotifier plugin, EntityPlayerMP target) {
    		this.target = target;
    		
    		//Create a fake player for our GUI
    		//This method basically just returns FakePlayerFactory.get(world, new GameProfile(UUID.randomUUID(), "MyFakePlayer"));
    		fakePlayer = plugin.getFakePlayerManager().getFakePlayer(target.getServerWorld());
    		
    		ItemStack filler = new ItemStack(Blocks.GLASS_PANE, 1, 0);
    		
    		/*for(int i=0; i < 9*5; i++) {
    			this.inventory.setInventorySlotContents(i, filler);
    		} */
    		
    		//Show the GUI to the player
    		target.displayGUIChest(fakePlayer.inventory);
    	}
    	
    	private void updateInventory() {
    		//Do some things here.
    
    		//Show the GUI to the player (Doing this makes the inventory flash very briefly every so often, before instantly closing again)
    		//target.displayGUIChest(fakePlayer.inventory);
    		
    		ResourceLocation location = new ResourceLocation("minecraft", "block.note.hat");
    		this.target.world.playSound(this.target, this.target.getPosition(), new SoundEvent(location), SoundCategory.MASTER, 1, 1);
    	}
    	
    	public void doTick() {
    		if(stepCounter >= counterMax) {
    			updateInventory();
    			stepCounter = 0;
    		}
    		
    		stepCounter++;
    	}
    }

     

    I read about fakeplayers not being able to do a lot of things, but didn't see anything about them not having an inventory. Was this perhaps just a part missing from the documentation? Or am I missing something else here?


    EDIT: nevermind, looking again at the code from https://github.com/LatvianModder/FTBUtilities/blob/master/src/main/java/com/feed_the_beast/ftbutilities/cmd/InvSeeInventory.java I realized that I messed it up by setting the method "isUsableByPlayer" to return false. Not quite sure why I did that either. (It's not shown in my example here that I use that extra class to wrap the inventory in because when posting here I thought it was obsolete. Mainly since in Spigot/Sponge development, it wasnt required)


    Problem solved anyway!

×
×
  • Create New...

Important Information

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