Jump to content

Help with enderchest type item


ND0322

Recommended Posts

I am looking to make a container that is opened through an item and has a seperate inventory for each player like an enderchest. I have created a capability to do this however it does not work. Here is the code:

 

ItemStackHandler:

public class CharmBagItemStackHandler extends ItemStackHandler implements CharmBagItemHandler {
	public int size;
	public CharmBagItemStackHandler(int size) {
		super(size);
	}
	
	public CharmBagItemStackHandler() {
		super(9);
	}

	public void copyFrom(CharmBagItemStackHandler source) {
		this.size = source.size;
		
	}
	
	

}

Capability provider(the code is a little messy):

public class CharmBagProvider implements ICapabilityProvider, INBTSerializable<CompoundTag>{
	public static Capability<CharmBagItemStackHandler> Handler = CapabilityManager.get(new CapabilityToken<>(){});
	
	private CharmBagItemStackHandler inventory = new CharmBagItemStackHandler(9);
	private Player player;
	
	public static CharmBagItemStackHandler CHARMBAG = null;
	private LazyOptional<CharmBagItemHandler> lazyOptional = LazyOptional.of(() -> inventory);
	
	
	@Nonnull
    private CharmBagItemStackHandler createHandler() {
        if (CHARMBAG == null) {
            CHARMBAG = new CharmBagItemStackHandler();
        }
        return CHARMBAG;
    }

	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
		
		
		if (cap == Handler) {
	            lazyOptional.cast();
	        }
	        return LazyOptional.empty();
	      
	}

	@Override
	public CompoundTag serializeNBT() {
		return inventory.serializeNBT();
	}

	@Override
	public void deserializeNBT(CompoundTag nbt) {
		inventory.deserializeNBT(nbt);
		
	}

	


}

Container:

public class CharmsBagContainer extends AbstractContainerMenu{
	private final ItemStack charmBag;
    private final Level level;
    
    
    
    // Client Constructor
    public CharmsBagContainer(int id, Inventory playerInv) {
    	this(id, playerInv, playerInv.player.getMainHandItem());
    }

    // Server constructor
    public CharmsBagContainer(int id, Inventory inventory, ItemStack item) {
        super(ContainerInit.CHARM_CONTAINER.get(), id);
       
        this.level = inventory.player.level;
        this.charmBag = item;
        Player player = inventory.player;     
      
        
        
       System.out.println(player.getCapability(CharmBagProvider.Handler).isPresent());
       player.getCapability(CharmBagProvider.Handler).ifPresent(handler -> {
            for(int i = 0; i < 3; ++i) {
                for(int j = 0; j < 3; ++j) {
                    this.addSlot(new SlotItemHandler(handler, j + i * 3, 44 + j * 18, 16 + i * 18));
                }
            }
        });
     
        

      
        
        this.addPlayerHotbar(inventory);
        this.addPlayerInventory(inventory);
      
        
        
    }
    
    private void addPlayerInventory(Inventory inv){
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 9; ++j) {
                this.addSlot(new Slot(inv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }
    }
    

    private void addPlayerHotbar(Inventory inv){
        for(int k = 0; k < 9; ++k) {
            this.addSlot(new Slot(inv, k, 8 + k * 18, 142));
        }
    }

   
    @Override
    public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
        ItemStack toReturn = ItemStack.EMPTY;
        Slot slot = this.getSlot(pIndex);
        if(slot != null && slot.hasItem()){
            ItemStack stack = slot.getItem();
            toReturn = stack.copy();
            if(pIndex < 9){
                if(!this.moveItemStackTo(stack, 9, this.slots.size(), true)){
                    return ItemStack.EMPTY;
                }
            }else if(!this.moveItemStackTo(stack, 0, 9, false)){
                return ItemStack.EMPTY;
            }

            if(!stack.isEmpty()){
                slot.set(ItemStack.EMPTY);
            }else{
                slot.setChanged();
            }
        }
        return toReturn;
    }
   
	@Override
	public boolean stillValid(Player player) {
		
		return player.getMainHandItem().is(this.charmBag.getItem());
	}

EventHandler:

@SubscribeEvent
	public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event){
        if (event.getObject() instanceof Player) {
            if (!event.getObject().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).isPresent()) {
                // The player does not already have this capability so we need to add the capability provider here
                event.addCapability(new ResourceLocation(truffleMod.MOD_ID, "max_health"), new MaxHealthProvider());
            }
        }
    }
	@SubscribeEvent
	public static void onPlayerCloned(PlayerEvent.Clone event) {
        if (event.isWasDeath()) {
            // We need to copyFrom the capabilities
            event.getOriginal().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).ifPresent(oldStore -> {
                event.getPlayer().getCapability(MaxHealthProvider.MAX_HEALTH_CHANGER).ifPresent(newStore -> {
                    newStore.copyFrom(oldStore);
                });
            });
        }
    }
    
	@SubscribeEvent
	public static void onRegisterCapabilities(RegisterCapabilitiesEvent event) {
        event.register(MaxHealth.class);
    }
}

My issue is probably with attaching the capability as I print to show if the player has the capability and it prints false.

Link to comment
Share on other sites

@SubscribeEvent
	public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event){
		
		
        if (event.getObject() instanceof Player) {
        	
        	 if (!event.getObject().getCapability(CharmBagProvider.Handler).isPresent()) {
        	
                event.addCapability(new ResourceLocation(truffleMod.MOD_ID, "charm_bag_cap"), new CharmBagProvider());
                System.out.println("added");
            }
        	System.out.println("eventRan");
        }
    }

I have put two prints to check that the event is called and both are called

Edited by ND0322
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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hey. I created shield mod like kaupenjoe did on his course but blocking animation doesn't work. Forge 1.20.1. Here's some information: Mod structure image and code: https://ibb.co/z5Y1G26 https://www.online-java.com/crhUHmPoJt
    • i was playing forge modded singleplayer server then i added optifine to mods but it crashed with optifine is there anything to do without deleting optifine  crash report: https://paste.ee/p/Z13LO
    • package com.projectmushroom.lavapotions.item; import java.util.List;   import com.projectmushroom.lavapotions.LavaPotions;   import net.minecraft.core.BlockPos; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.stats.Stats; import net.minecraft.tags.FluidTags; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.AreaEffectCloud; import net.minecraft.world.entity.boss.enderdragon.EnderDragon; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemUtils; import net.minecraft.world.item.Items; import net.minecraft.world.item.alchemy.PotionUtils; import net.minecraft.world.item.alchemy.Potions; import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult;   public class ReinforcedBottle extends Item { public ReinforcedBottle(Item.Properties p_40648_) { super(p_40648_); }   public InteractionResultHolder<ItemStack> use(Level p_40656_, Player p_40657_, InteractionHand p_40658_) { List<AreaEffectCloud> list = p_40656_.getEntitiesOfClass(AreaEffectCloud.class, p_40657_.getBoundingBox().inflate(2.0D), (p_40650_) -> { return p_40650_ != null && p_40650_.isAlive() && p_40650_.getOwner() instanceof EnderDragon; }); ItemStack itemstack = p_40657_.getItemInHand(p_40658_); if (!list.isEmpty()) { AreaEffectCloud areaeffectcloud = list.get(0); areaeffectcloud.setRadius(areaeffectcloud.getRadius() - 0.5F); p_40656_.playSound((Player)null, p_40657_.getX(), p_40657_.getY(), p_40657_.getZ(), SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.NEUTRAL, 1.0F, 1.0F); p_40656_.gameEvent(p_40657_, GameEvent.FLUID_PICKUP, p_40657_.blockPosition()); return InteractionResultHolder.sidedSuccess(this.turnBottleIntoItem(itemstack, p_40657_, new ItemStack(Items.DRAGON_BREATH)), p_40656_.isClientSide()); } else { HitResult hitresult = getPlayerPOVHitResult(p_40656_, p_40657_, ClipContext.Fluid.SOURCE_ONLY); if (hitresult.getType() == HitResult.Type.MISS) { return InteractionResultHolder.pass(itemstack); } else { if (hitresult.getType() == HitResult.Type.BLOCK) { BlockPos blockpos = ((BlockHitResult)hitresult).getBlockPos(); if (!p_40656_.mayInteract(p_40657_, blockpos)) { return InteractionResultHolder.pass(itemstack); }   if (p_40656_.getFluidState(blockpos).is(FluidTags.LAVA)) { p_40656_.playSound(p_40657_, p_40657_.getX(), p_40657_.getY(), p_40657_.getZ(), SoundEvents.BOTTLE_FILL, SoundSource.NEUTRAL, 1.0F, 1.0F); p_40656_.gameEvent(p_40657_, GameEvent.FLUID_PICKUP, blockpos); return InteractionResultHolder.sidedSuccess(this.turnBottleIntoItem(itemstack, p_40657_,(new ItemStack(LavaPotions.REINFORCED_BOTTLE), .LAVA)), p_40656_.isClientSide()); } } return InteractionResultHolder.pass(itemstack); } } }   protected ItemStack turnBottleIntoItem(ItemStack p_40652_, Player p_40653_, ItemStack p_40654_) { p_40653_.awardStat(Stats.ITEM_USED.get(this)); return ItemUtils.createFilledResult(p_40652_, p_40653_, p_40654_); } }   This is what I have so far, it's pretty much just the waterbottle code but I'm just trying to figure out how to get it to pick up lava rather than water
    • I never started my own server manually but I follow every single step i saw in a youtube video.
  • Topics

×
×
  • Create New...

Important Information

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