Jump to content

Recommended Posts

Posted

There are the following container slots, and items can be displayed in the slots by changing the backpack.

However, if you try to move an item in the backpack slot, you will not be able to pick it up, and if you move it from the inventory to the backpack slot, the item will disappear.

I looked through the code and couldn't figure out the cause of this bug. How can I get it to work properly?

 

Container class:

Spoiler

public class BelongingsUMUPlayerContainer extends AbstractUMUPlayerContainer {

	private static final UMUEquipmentSlotType[] VALID_EQUIPMENT_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.HEAD, UMUEquipmentSlotType.CHEST, UMUEquipmentSlotType.LEGS, UMUEquipmentSlotType.FEET
	};
	private static final UMUEquipmentSlotType[] VALID_RING_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.MAINRING1, UMUEquipmentSlotType.MAINRING2, UMUEquipmentSlotType.OFFRING1, UMUEquipmentSlotType.OFFRING2
	};
	private static final UMUEquipmentSlotType[] VALID_MISCELLANEOUS_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.ENCYCLOPEDIA, UMUEquipmentSlotType.MAP
	};

	public BelongingsUMUPlayerContainer(int id, PlayerInventory playerInventory, PacketBuffer extraData) {
		this(UMUContainers.BELONGING_INVENTORY.get(), id, playerInventory, playerInventory.player);
	}

	public BelongingsUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id, playerInventory, player);

		// index : 36 - 39
		for (int i = 0; i < 4; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 39 - i, 186, 18 + i * 18, VALID_EQUIPMENT_SLOTS[i]));
		}
		this.addSlot(new OffhandSlot(playerInventory, 40, 255, 72));
		this.addSlot(new EquipmentSlot(playerInventory, 41, 293, 27, UMUEquipmentSlotType.NECKLACE));
		// index : 42 - 45
		for (int iU = 0; iU < 2; iU++) {
			for (int iV = 0; iV < 2; iV++) {
				this.addSlot(new EquipmentSlot(playerInventory, 42 + iU * 2 + iV, 282 + iU * 18 + 4, 48 + iV * 18, VALID_RING_SLOTS[iU * 2 + iV]));
			}
		}
		for (int i = 0; i < 2; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 46 + i, 330, 72 + i * 18, VALID_MISCELLANEOUS_SLOTS[i]));
		}

		for (int iV = 0; iV < 4; iV++) {
			for (int iU = 0; iU < 9; iU++) {
				Slot slot = this.addSlot(new BackpackSlot(
						new ItemStackHandler(36), new ItemStackHandler(36), iU + iV * 9, 8 + iU * 18, 18 + iV * 18, player, iV + 1)
				);
				if (slot instanceof AbstractBelongingsSlot) {
					AbstractBelongingsSlot belongingsSlot = (AbstractBelongingsSlot) slot;
					belongingsSlot.updateInventory(38, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
					this.addListener(belongingsSlot);
				}
			}
		}
	}
}

 


public abstract class AbstractUMUPlayerContainer extends Container {
	protected final PlayerEntity player;

	protected AbstractUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id);

		this.player = player;

		//index : 9 - 35
		for(int iV = 0; iV < 3; iV++) {
			for(int iU = 0; iU < 9; iU++) {
				this.addSlot(new Slot(playerInventory, iU + (iV + 1) * 9, 186 + iU * 18, 104 + iV * 18));
			}
		}
		for(int i = 0; i < 9; i++) {
			this.addSlot(new Slot(playerInventory, i, 186 + i * 18, 162));
		}
	}

	@Override
	@SuppressWarnings("NullableProblems")
	public boolean canInteractWith(PlayerEntity playerIn) {
		return true;
	}

	@Override
	@SuppressWarnings({"ConstantConditions", "NullableProblems"})
	public final ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		Slot slot = this.inventorySlots.get(index);
		ItemStack returnStack = ItemStack.EMPTY;

		if (slot == null || !slot.getHasStack()) return returnStack;

		ItemStack currentStack = slot.getStack();
		returnStack = currentStack.copy();

		if (index >= 0 && index < 27) {
			if (!this.mergeItemStack(currentStack, 27, 36, false)) {
				return ItemStack.EMPTY;
			}
		} else if (index >= 27 && index < 36) {
			if (!this.mergeItemStack(currentStack, 0, 27, false)) {
				return ItemStack.EMPTY;
			}
		} else {
			if (!this.mergeItemStack(currentStack, 0, 36, false)) {
				return ItemStack.EMPTY;
			}
		}

		if (currentStack.isEmpty()) {
			slot.putStack(ItemStack.EMPTY);
		} else {
			slot.onSlotChanged();
		}

		if (currentStack.getCount() == returnStack.getCount()) {
			return ItemStack.EMPTY;
		}

		return returnStack;
	}
}

Slot class:

Spoiler

public class BackpackSlot extends AbstractBelongingsSlot {
	private final PlayerEntity player;
	private final int size;

	public BackpackSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition, PlayerEntity player, int size) {
		super(baseItemHandler, inventoryItemHandler, index, xPosition, yPosition);
		this.player = player;
		this.size = size;
	}

	@Override
	public boolean isEnabled() {
		this.updateInventory(38, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
		if (UMUEntityUtil.hasBackpack(player)) {
			return UMUEntityUtil.getBackpackSize(Minecraft.getInstance().player) >= size;
		}

		return false;
	}

	@Override
	public void updateInventory(int slotIndex, ItemStack itemStack) {
		if (slotIndex != 38) return;
		if (itemStack.getItem() == UMUItems.BACKPACK.get()) {
			this.inventoryItemHandler = itemStack
					.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).orElse(new ItemStackHandler(36));
			return;
		}
		this.inventoryItemHandler = new ItemStackHandler(36);
	}
}

 


public abstract class AbstractBelongingsSlot extends SlotItemHandler implements IContainerListener {
	protected IItemHandler inventoryItemHandler;

	public AbstractBelongingsSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition) {
		super(baseItemHandler, index, xPosition, yPosition);
		this.inventoryItemHandler = inventoryItemHandler;
	}

	@Override
	public IItemHandler getItemHandler() {
		return this.inventoryItemHandler;
	}

	@Override
	public void sendAllContents(Container containerToSend, NonNullList<ItemStack> itemsList) {
	}

	@Override
	public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) {
		this.updateInventory(slotInd, stack);
	}

	@Override
	public void sendWindowProperty(Container containerIn, int varToUpdate, int newValue) {
	}

	public abstract void updateInventory(int slotIndex, ItemStack itemStack);
}

 

 

Posted (edited)

I've fixed the code so that item operations can now be done normally. However, the backpack inventory operations that you were equipped with when you opened the inventory are saved, but the inventory operations for the backpack that you replaced in that inventory after opening the inventory are not saved. I was wondering if the inventory I changed later wouldn't be saved because the inventory I saved when I opened the container was saved somewhere else, but I couldn't find a match by tracing the code in the IDE. did. Does anyone know why the modified inventory isn't saved?

 

Container class:

Spoiler


public class BelongingsUMUPlayerContainer extends AbstractUMUPlayerContainer {
	private NonNullList<ItemStack> inventoryItemStacks = NonNullList.create();

	private static final UMUEquipmentSlotType[] VALID_EQUIPMENT_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.HEAD, UMUEquipmentSlotType.CHEST, UMUEquipmentSlotType.LEGS, UMUEquipmentSlotType.FEET
	};
	private static final UMUEquipmentSlotType[] VALID_RING_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.MAINRING1, UMUEquipmentSlotType.MAINRING2, UMUEquipmentSlotType.OFFRING1, UMUEquipmentSlotType.OFFRING2
	};
	private static final UMUEquipmentSlotType[] VALID_MISCELLANEOUS_SLOTS = new UMUEquipmentSlotType[]{
			UMUEquipmentSlotType.ENCYCLOPEDIA, UMUEquipmentSlotType.MAP
	};

	public BelongingsUMUPlayerContainer(int id, PlayerInventory playerInventory, PacketBuffer extraData) {
		this(UMUContainers.BELONGING_INVENTORY.get(), id, playerInventory, playerInventory.player);
	}

	public BelongingsUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id, playerInventory, player);

		// index : 36 - 39
		for (int i = 0; i < 4; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 39 - i, 186, 18 + i * 18, VALID_EQUIPMENT_SLOTS[i]));
		}
		this.addSlot(new OffhandSlot(playerInventory, 40, 255, 72));
		this.addSlot(new EquipmentSlot(playerInventory, 41, 293, 27, UMUEquipmentSlotType.NECKLACE));
		// index : 42 - 45
		for (int iU = 0; iU < 2; iU++) {
			for (int iV = 0; iV < 2; iV++) {
				this.addSlot(new EquipmentSlot(playerInventory, 42 + iU * 2 + iV, 282 + iU * 18 + 4, 48 + iV * 18, VALID_RING_SLOTS[iU * 2 + iV]));
			}
		}
		for (int i = 0; i < 2; i++) {
			this.addSlot(new EquipmentSlot(playerInventory, 46 + i, 330, 72 + i * 18, VALID_MISCELLANEOUS_SLOTS[i]));
		}

		for (int iV = 0; iV < 4; iV++) {
			for (int iU = 0; iU < 9; iU++) {
				Slot slot = this.addSlot(new BackpackSlot(
						new ItemStackHandler(36), new ItemStackHandler(36), iU + iV * 9, 8 + iU * 18, 18 + iV * 18, player, iV + 1)
				);
				if (slot instanceof AbstractBelongingsSlot) {
					((AbstractBelongingsSlot) slot).updateInventory(37, this.player.getItemStackFromSlot(EquipmentSlotType.CHEST));
				}
			}
		}
	}

	@Override
	public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
		this.inventorySlots.subList(48, 83).stream()
				.filter(s -> s instanceof AbstractBelongingsSlot)
				.forEach(s -> ((AbstractBelongingsSlot) s)
						.updateInventory(slotId, player.inventory.getItemStack()));

		return super.slotClick(slotId, dragType, clickTypeIn, player);
	}
}


public abstract class AbstractUMUPlayerContainer extends Container {
	protected final PlayerEntity player;

	protected AbstractUMUPlayerContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, PlayerEntity player) {
		super(type, id);

		this.player = player;

		//index : 9 - 35
		for(int iV = 0; iV < 3; iV++) {
			for(int iU = 0; iU < 9; iU++) {
				this.addSlot(new Slot(playerInventory, iU + (iV + 1) * 9, 186 + iU * 18, 104 + iV * 18));
			}
		}
		for(int i = 0; i < 9; i++) {
			this.addSlot(new Slot(playerInventory, i, 186 + i * 18, 162));
		}
	}

	@Override
	@SuppressWarnings("NullableProblems")
	public boolean canInteractWith(PlayerEntity playerIn) {
		return true;
	}

	@Override
	@SuppressWarnings({"ConstantConditions", "NullableProblems"})
	public final ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
		Slot slot = this.inventorySlots.get(index);
		ItemStack returnStack = ItemStack.EMPTY;

		if (slot == null || !slot.getHasStack()) return returnStack;

		ItemStack currentStack = slot.getStack();
		returnStack = currentStack.copy();

		if (index >= 0 && index < 27) {
			if (!this.mergeItemStack(currentStack, 27, 36, false)) {
				return ItemStack.EMPTY;
			}
		} else if (index >= 27 && index < 36) {
			if (!this.mergeItemStack(currentStack, 0, 27, false)) {
				return ItemStack.EMPTY;
			}
		} else {
			if (!this.mergeItemStack(currentStack, 0, 36, false)) {
				return ItemStack.EMPTY;
			}
		}

		if (currentStack.isEmpty()) {
			slot.putStack(ItemStack.EMPTY);
		} else {
			slot.onSlotChanged();
		}

		if (currentStack.getCount() == returnStack.getCount()) {
			return ItemStack.EMPTY;
		}

		return returnStack;
	}
}

 

Slot class:

Spoiler


public class BackpackSlot extends AbstractBelongingsSlot {
	private final PlayerEntity player;
	private final int size;

	public BackpackSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition, PlayerEntity player, int size) {
		super(baseItemHandler, inventoryItemHandler, index, xPosition, yPosition);
		this.player = player;
		this.size = size;
	}

	@Override
	public boolean isEnabled() {
		if (UMUEntityUtil.hasBackpack(player)) {
			return UMUEntityUtil.getBackpackSize(Minecraft.getInstance().player) >= size;
		}

		return false;
	}

	@Override
	public void updateInventory(int slotIndex, ItemStack itemStack) {
		if (slotIndex != 37) return;
		if (itemStack.getItem() == UMUItems.BACKPACK.get()) {
			this.inventoryItemHandler = itemStack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
					.orElse(new ItemStackHandler(36));
		} else {
			this.inventoryItemHandler = new ItemStackHandler(36);
		}
	}
}


public abstract class AbstractBelongingsSlot extends SlotItemHandler {
	protected IItemHandler inventoryItemHandler;

	public AbstractBelongingsSlot(IItemHandler baseItemHandler, IItemHandler inventoryItemHandler, int index, int xPosition, int yPosition) {
		super(baseItemHandler, index, xPosition, yPosition);
		this.inventoryItemHandler = inventoryItemHandler;
	}

	@Override
	public IItemHandler getItemHandler() {
		return this.inventoryItemHandler;
	}

	public abstract void updateInventory(int slotIndex, ItemStack itemStack);
}

 

 

Edited by Zemelua

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Was launching forge for the first time and it crashed: Processor failed, invalid outputs: /home/frenchy/.local/share/PrismLauncher/libraries/net/minecraft/client/1.20.1-20230612.114412/client-1.20.1-20230612.114412-slim.jar Expected: de86b035d2da0f78940796bb95c39a932ed84834 Actual: a8fb49bc364562847d6e7e6775e3a1b3f6b2bb05 /home/frenchy/.local/share/PrismLauncher/libraries/net/minecraft/client/1.20.1-20230612.114412/client-1.20.1-20230612.114412-extra.jar Expected: 8c5a95cbce940cfdb304376ae9fea47968d02587 Actual: cf941ba69e11f5a9de15d0c319d61854c456a116 No idea why I think it's because I'm on linux or something  
    • Game crashed on launch with:  Processor failed, invalid outputs: /home/user/.local/share/PrismLauncher/libraries/net/minecraft/client/1.20.1-20230612.114412/client-1.20.1-20230612.114412-slim.jar Expected: de86b035d2da0f78940796bb95c39a932ed84834 Actual: a8fb49bc364562847d6e7e6775e3a1b3f6b2bb05 /home/user/.local/share/PrismLauncher/libraries/net/minecraft/client/1.20.1-20230612.114412/client-1.20.1-20230612.114412-extra.jar Expected: 8c5a95cbce940cfdb304376ae9fea47968d02587 Actual: cf941ba69e11f5a9de15d0c319d61854c456a116 No idea why, but im on linux which might matter
    • PixelmonGo est un incroyable serveur Pixelmon disponible grâce à notre launcher en version 1.16.5 Ce serveur accepte les joueurs premiums comme les cracks ! Un gameplay unique vous attend.. Rejoignez nous dès maintenant ! Site: https://pixelmongo.fr/ Launcher: https://pixelmongo.fr/launcher/ Discord: https://discord.gg/pixelmongo Découvrez notre serveur minecraft Pixelmon moddé basé sur un univers mélangeant Minecraft et Pokémon. Plus de 900 Pokémon à capturer ainsi que des fusions unique au serveur, un hôtel des ventes, un monde aventure reproduisant l'aventure de sinnoh, Explorez des donjons de chaque team maléfique au début de votre aventure, accomplissez des quêtes et remportez des récompenses quotidiennes. Revivez vos meilleurs souvenirs Pokémon au sein d'une communauté multijoueur dynamique. Rejoignez le meilleur serveur minecraft pixelmon français des maintenant en téléchargeant notre launcher. Pixelmongo est la référence en serveur pixelmon en France ! Présentation Amenez votre Minecraft dans le monde des Pokémon, ou des Pokémon dans votre monde Minecraft ! Avec Pixelmon, découvrez votre monde Minecraft sous un nouvel angle. Pixelmon est un mod populaire pour Minecraft qui permet aux joueurs d'attraper, d'entraîner et de combattre des Pokémon dans le monde de Minecraft. Développé par un groupe de fans dévoués, le mod ajoute une large gamme de créatures Pokémon au jeu. Il possède des fonctionnalités et des mécanismes uniques qui en font une expérience amusante et engageante pour les joueurs de tous âges. Le mod Pixelmon est disponible pour les mondes Minecraft solo et multijoueur et peut être téléchargé et installé à l'aide de divers lanceurs et modpacks. Une fois installé, les joueurs peuvent explorer le monde et rencontrer des Pokémon dans la nature, se battre avec d'autres entraîneurs et créer leur propre équipe de créatures puissantes. L'une des fonctionnalités clés de Pixelmon est la possibilité de capturer et d'entraîner des Pokémon en utilisant diverses méthodes. Les joueurs peuvent fabriquer des Pokéballs et les utiliser pour capturer des Pokémon sauvages, qui peuvent ensuite être entraînés et améliorés au fil du temps. Chaque Pokémon a des capacités et des mouvements uniques, ce qui rend important pour les joueurs de choisir la bonne équipe de créatures pour chaque combat. Capturez des Pokémons, constituez une équipe, entraînez-les et remportez des combats contre d'autres joueurs ! Dans un univers reprenant les standards du jeu Nintendo original : Dresseurs, centres Pokémon, mais aussi fossiles et matériaux divers. Les Pokémon comme dans le jeu original sont classés par type (Insecte, Ténèbres, Dragon, Électrique, Combat, Feu, Vol, Spectre, Plante, Sol, Glace, Normal, Poison, Psy, Pierre, Acier, Eau), ce qui définira les faiblesses et les spécificités des Pokémons. Par exemple, un Pokémon de type Feu subira deux fois plus de dégâts si l'attaque du Pokémon ennemi est de type Glace. Le mod possède 900 Pokémon différents plus ou moins rares qui apparaîtront en fonction de leur environnement (jour, nuit et biomes). Les Pokémon évoluent en fonction de leur niveau (jusqu'à 100). Plus leur niveau est élevé, plus ils seront forts et auront des attaques plus puissantes. Pour augmenter le niveau de votre Pokémon, et ainsi évoluer, vous devrez combattre d'autres Pokémon et les vaincre. Plus vos adversaires sont forts, plus ils vous rapporteront de l'expérience. Les Pokémons ont également leurs propres statistiques (attaque, défense, vitesse, vitesse d'attaque et vitesse de défense). Ils peuvent également avoir des tailles et des formes différentes, et peuvent occasionnellement vous donner des objets une fois tués. Pour les capturer, vous devrez utiliser des pokéballs, qui selon leur forme seront plus ou moins efficaces. Pixelmon est un mod amusant et engageant pour Minecraft qui ajoute une touche unique et passionnante au jeu. Avec sa large gamme de fonctionnalités et de mécanismes, il offre aux joueurs des possibilités infinies d'exploration et de plaisir, ce qui en fait un choix populaire pour les fans de Minecraft et de Pokemon.  
    • Hi all,  I have the following issue: I'd like to parse some json using gson and rely on the (somewhat new) java record types. This is supported in gson 2.10+. Gson is already a dependency used by minecraft, however it's 2.8.x for 1.19.2 which I'm targeting at the moment. My idea was to include the newer version of the library in my mod and then shadow it so it gets used inside the scope of my mod instead of the older 2.8. This works fine for building the jar: If I decompile my mod.jar, I can see that it's correctly using the shadowed classes. However obviously when using the runClient intellj config, the shadowing doesn't get invoked. Is there any way of invoking shadow when using runClient, or am I on the wrong track and there's a better way of doing this entirely?   Thanks in advance!   Edit: After some further thinking, I've come up with this abomination:   build.gradle // New task for extracting the result of shadowJar into the classes directory // This includes our shadowed gson jar tasks.register("extractShadowJar", Copy) { // Depend on shadowJar so we always use the up to date version of the shadowed content dependsOn shadowJar from(zipTree(shadowJar.archiveFile)) { // filter to copy only our code (and ignore assets, META-INF, etc) // Also copies gson as it gets shadowed into com.oppendev.shadow.gson include "com/**" } duplicatesStrategy(DuplicatesStrategy.INCLUDE) into("$buildDir/classes/java/main") // Extract into the classes directory } // Tell gradle to invoke our new task before executing any java code. This way we ensure that we use the shadowed gson tasks.withType(JavaExec).configureEach { dependsOn(extractShadowJar) } // Shadow config shadowJar { relocate 'com.google.gson', 'com.oppendev.shadow.gson' configurations = [project.configurations.runtimeClasspath] zip64 true dependencies { include(dependency('com.google.code.gson:gson')) } } Is this a reasonable thing to do? Is this completely cursed and I should burn in dev ops hell? Is there a better way to do this? Feel free to grill me in the replies
    • Yep I did upgrade just because it showed me a new version available.  I'll redownload the mod list and make sure anything works.  Thanks!
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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