mirror of
https://github.com/GeyserMC/Geyser.git
synced 2025-12-19 14:59:27 +00:00
Fix: Looms modifying default data components causing visual bugs
Closes https://github.com/GeyserMC/Geyser/issues/5273
This commit is contained in:
@@ -50,6 +50,7 @@ import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.ItemSta
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.recipe.display.slot.SlotDisplay;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Data
|
||||
public class GeyserItemStack {
|
||||
@@ -169,9 +170,9 @@ public class GeyserItemStack {
|
||||
return value;
|
||||
}
|
||||
|
||||
public <T> T getComponentOrFallback(@NonNull DataComponentType<T> type, T def) {
|
||||
public <T> T getComponentElseGet(@NonNull DataComponentType<T> type, Supplier<T> supplier) {
|
||||
T value = getComponent(type);
|
||||
return value == null ? def : value;
|
||||
return value == null ? supplier.get() : value;
|
||||
}
|
||||
|
||||
public int getNetId() {
|
||||
|
||||
@@ -47,7 +47,6 @@ import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemEnchantments;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.item.component.Unbreakable;
|
||||
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
|
||||
|
||||
import java.util.List;
|
||||
@@ -420,7 +419,7 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
|
||||
}
|
||||
|
||||
private int getRepairCost(GeyserItemStack itemStack) {
|
||||
return itemStack.getComponentOrFallback(DataComponentType.REPAIR_COST, 0);
|
||||
return itemStack.getComponentElseGet(DataComponentType.REPAIR_COST, () -> 0);
|
||||
}
|
||||
|
||||
private boolean hasDurability(GeyserItemStack itemStack) {
|
||||
@@ -431,6 +430,6 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
|
||||
}
|
||||
|
||||
private int getDamage(GeyserItemStack itemStack) {
|
||||
return itemStack.getComponentOrFallback(DataComponentType.DAMAGE, 0);
|
||||
return itemStack.getComponentElseGet(DataComponentType.DAMAGE, () -> 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,7 +314,7 @@ public final class BundleInventoryTranslator {
|
||||
return Fraction.ONE;
|
||||
}
|
||||
}
|
||||
return Fraction.getFraction(1, itemStack.getComponentOrFallback(DataComponentType.MAX_STACK_SIZE, itemStack.asItem().defaultMaxStackSize()));
|
||||
return Fraction.getFraction(1, itemStack.getComponentElseGet(DataComponentType.MAX_STACK_SIZE, () -> itemStack.asItem().defaultMaxStackSize()));
|
||||
}
|
||||
|
||||
public static int capacityForItemStack(Fraction bundleWeight, GeyserItemStack itemStack) {
|
||||
|
||||
@@ -93,10 +93,8 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
||||
PATTERN_TO_INDEX.put("vhr", index++);
|
||||
PATTERN_TO_INDEX.put("hhb", index++);
|
||||
PATTERN_TO_INDEX.put("bo", index++);
|
||||
index++; // Bordure indented, does not appear to exist in Bedrock?
|
||||
PATTERN_TO_INDEX.put("gra", index++);
|
||||
PATTERN_TO_INDEX.put("gru", index);
|
||||
// Bricks do not appear to be a pattern on Bedrock, either
|
||||
}
|
||||
|
||||
public LoomInventoryTranslator() {
|
||||
@@ -120,7 +118,7 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
||||
|
||||
@Override
|
||||
protected boolean shouldHandleRequestFirst(ItemStackRequestAction action, Inventory inventory) {
|
||||
// If the LOOM_MATERIAL slot is not empty, we are crafting a pattern that does not come from an item
|
||||
// If the LOOM_MATERIAL slot is empty, we are crafting a pattern that does not come from an item
|
||||
return action.getType() == ItemStackRequestActionType.CRAFT_LOOM && inventory.getItem(2).isEmpty();
|
||||
}
|
||||
|
||||
@@ -135,10 +133,6 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
||||
return rejectRequest(request);
|
||||
}
|
||||
|
||||
// Get the patterns compound tag
|
||||
List<NbtMap> newBlockEntityTag = craftData.getResultItems()[0].getTag().getList("Patterns", NbtType.COMPOUND);
|
||||
// Get the pattern that the Bedrock client requests - the last pattern in the Patterns list
|
||||
NbtMap pattern = newBlockEntityTag.get(newBlockEntityTag.size() - 1);
|
||||
String bedrockPattern = ((CraftLoomAction) headerData).getPatternId();
|
||||
|
||||
// Get the Java index of this pattern
|
||||
@@ -146,6 +140,12 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
||||
if (index == -1) {
|
||||
return rejectRequest(request);
|
||||
}
|
||||
|
||||
// Get the patterns compound tag
|
||||
List<NbtMap> newBlockEntityTag = craftData.getResultItems()[0].getTag().getList("Patterns", NbtType.COMPOUND);
|
||||
// Get the pattern that the Bedrock client requests - the last pattern in the Patterns list
|
||||
NbtMap pattern = newBlockEntityTag.get(newBlockEntityTag.size() - 1);
|
||||
|
||||
// Java's formula: 4 * row + col
|
||||
// And the Java loom window has a fixed row/width of four
|
||||
// So... Number / 4 = row (so we don't have to bother there), and number % 4 is our column, which leads us back to our index. :)
|
||||
@@ -156,10 +156,7 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
|
||||
inputCopy.setNetId(session.getNextItemNetId());
|
||||
BannerPatternLayer bannerPatternLayer = BannerItem.getJavaBannerPattern(session, pattern); // TODO
|
||||
if (bannerPatternLayer != null) {
|
||||
List<BannerPatternLayer> patternsList = inputCopy.getComponent(DataComponentType.BANNER_PATTERNS);
|
||||
if (patternsList == null) {
|
||||
patternsList = new ArrayList<>();
|
||||
}
|
||||
List<BannerPatternLayer> patternsList = new ArrayList<>(inputCopy.getComponentElseGet(DataComponentType.BANNER_PATTERNS, ArrayList::new));
|
||||
patternsList.add(bannerPatternLayer);
|
||||
inputCopy.getOrCreateComponents().put(DataComponentType.BANNER_PATTERNS, patternsList);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user