9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

fix: updates for Fabric 1.21.5

This commit is contained in:
William278
2025-05-26 20:47:26 +01:00
parent 6a67d1bbe0
commit c51ba85f38
4 changed files with 105 additions and 32 deletions

View File

@@ -49,6 +49,7 @@ import net.minecraft.stat.StatType;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameMode;
import net.william278.desertwell.util.ThrowingConsumer;
import net.william278.husksync.FabricHuskSync;
import net.william278.husksync.HuskSync;
@@ -188,7 +189,11 @@ public abstract class FabricData implements Data {
for (int slot = 0; slot < player.getInventory().size(); slot++) {
player.getInventory().setStack(slot, items[slot] == null ? ItemStack.EMPTY : items[slot]);
}
player.getInventory().selectedSlot = heldItemSlot;
//#if MC<12105
//$$ player.getInventory().selectedSlot = heldItemSlot;
//#else
player.getInventory().setSelectedSlot(heldItemSlot);
//#endif
player.playerScreenHandler.sendContentUpdates();
player.getInventory().updateItems();
}
@@ -888,7 +893,11 @@ public abstract class FabricData implements Data {
@Override
public void apply(@NotNull FabricUser user, @NotNull FabricHuskSync plugin) throws IllegalStateException {
user.getPlayer().changeGameMode(net.minecraft.world.GameMode.byName(gameMode));
//#if MC<12105
//$$ user.getPlayer().changeGameMode(net.minecraft.world.GameMode.byName(gameMode));
//#else
user.getPlayer().changeGameMode(net.minecraft.world.GameMode.byId(gameMode));
//#endif
}
}

View File

@@ -76,17 +76,29 @@ public abstract class FabricSerializer {
final FabricHuskSync plugin = (FabricHuskSync) getPlugin();
final NbtCompound root;
try {
root = StringNbtReader.parse(serialized);
//#if MC<12105
//$$ root = StringNbtReader.parse(serialized);
//#else
root = StringNbtReader.readCompound(serialized);
//#endif
} catch (Throwable e) {
throw new DeserializationException("Failed to read item NBT from string (%s)".formatted(serialized), e);
}
// Deserialize the inventory data
final NbtCompound items = root.contains(ITEMS_TAG) ? root.getCompound(ITEMS_TAG) : null;
//#if MC<12105
//$$ final NbtCompound items = root.contains(ITEMS_TAG) ? root.getCompound(ITEMS_TAG) : null;
//$$ return FabricData.Items.Inventory.from(
//$$ items != null ? getItems(items, dataMcVersion, plugin) : new ItemStack[INVENTORY_SLOT_COUNT],
//$$ root.contains(HELD_ITEM_SLOT_TAG) ? root.getInt(HELD_ITEM_SLOT_TAG) : 0
//$$ );
//#else
final NbtCompound items = root.contains(ITEMS_TAG) ? root.getCompoundOrEmpty(ITEMS_TAG) : null;
return FabricData.Items.Inventory.from(
items != null ? getItems(items, dataMcVersion, plugin) : new ItemStack[INVENTORY_SLOT_COUNT],
root.contains(HELD_ITEM_SLOT_TAG) ? root.getInt(HELD_ITEM_SLOT_TAG) : 0
items != null ? getItems(items, dataMcVersion, plugin) : new ItemStack[INVENTORY_SLOT_COUNT],
root.getInt(HELD_ITEM_SLOT_TAG, 0)
);
//#endif
}
@Override
@@ -121,7 +133,11 @@ public abstract class FabricSerializer {
throws DeserializationException {
final FabricHuskSync plugin = (FabricHuskSync) getPlugin();
try {
final NbtCompound items = StringNbtReader.parse(serialized);
//#if MC<12105
//$$ final NbtCompound items = StringNbtReader.parse(serialized);
//#else
final NbtCompound items = StringNbtReader.readCompound(serialized);
//#endif
return FabricData.Items.EnderChest.adapt(getItems(items, dataMcVersion, plugin));
} catch (Throwable e) {
throw new DeserializationException("Failed to read item NBT from string (%s)".formatted(serialized), e);
@@ -153,14 +169,26 @@ public abstract class FabricSerializer {
return upgradeItemStacks(tag, mcVersion, plugin);
}
final ItemStack[] contents = new ItemStack[tag.getInt("size")];
final NbtList itemList = tag.getList("items", NbtElement.COMPOUND_TYPE);
final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
//#if MC<12105
//$$ final ItemStack[] contents = new ItemStack[tag.getInt("size")];
//$$ final NbtList itemList = tag.getList("items", NbtElement.COMPOUND_TYPE);
//$$ itemList.forEach(element -> {
//$$ final NbtCompound compound = (NbtCompound) element;
//$$ contents[compound.getInt("Slot")] = decodeNbt(element, registryManager);
//$$ });
//#else
final ItemStack[] contents = new ItemStack[tag.getInt("size", 0)];
final NbtList itemList = tag.getListOrEmpty("items");
itemList.forEach(element -> {
final NbtCompound compound = (NbtCompound) element;
contents[compound.getInt("Slot")] = decodeNbt(element, registryManager);
int i = compound.getInt("Slot", -1);
if (i >= 0) {
contents[i] = decodeNbt(element, registryManager);
}
});
plugin.debug(Arrays.toString(contents));
//#endif
return contents;
} catch (Throwable e) {
throw new Serializer.DeserializationException("Failed to read item NBT string (%s)".formatted(tag), e);
@@ -199,19 +227,37 @@ public abstract class FabricSerializer {
@NotNull
private ItemStack @NotNull [] upgradeItemStacks(@NotNull NbtCompound items, @NotNull Version mcVersion,
@NotNull FabricHuskSync plugin) {
final int size = items.getInt("size");
final NbtList list = items.getList("items", NbtElement.COMPOUND_TYPE);
//#if MC<12105
//$$ final int size = items.getInt("size");
//$$ final NbtList list = items.getList("items", NbtElement.COMPOUND_TYPE);
//$$ final ItemStack[] itemStacks = new ItemStack[size];
//$$ final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
//$$ Arrays.fill(itemStacks, ItemStack.EMPTY);
//$$ for (int i = 0; i < size; i++) {
//$$ if (list.getCompound(i) == null) {
//$$ continue;
//$$ }
//$$ final NbtCompound compound = list.getCompound(i);
//$$ final int slot = compound.getInt("Slot");
//$$ itemStacks[slot] = decodeNbt(upgradeItemData(list.getCompound(i), mcVersion, plugin), registryManager);
//$$ }
//#else
final int size = items.getInt("size", 0);
final NbtList list = items.getListOrEmpty("items");
final ItemStack[] itemStacks = new ItemStack[size];
final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
Arrays.fill(itemStacks, ItemStack.EMPTY);
for (int i = 0; i < size; i++) {
if (list.getCompound(i) == null) {
final NbtCompound compound = list.getCompoundOrEmpty(i);
if (compound.isEmpty()) {
continue;
}
final NbtCompound compound = list.getCompound(i);
final int slot = compound.getInt("Slot");
itemStacks[slot] = decodeNbt(upgradeItemData(list.getCompound(i), mcVersion, plugin), registryManager);
final int slot = compound.getInt("Slot", -1);
if (slot >= 0) {
itemStacks[slot] = decodeNbt(upgradeItemData(compound, mcVersion, plugin), registryManager);
}
}
//#endif
return itemStacks;
}

View File

@@ -79,27 +79,41 @@ public interface FabricUserDataHolder extends UserDataHolder {
final PlayerInventory inventory = getPlayer().getInventory();
return Optional.of(FabricData.Items.Inventory.from(
getCombinedInventory(inventory),
inventory.selectedSlot
//#if MC<12105
//$$ inventory.selectedSlot
//#else
inventory.getSelectedSlot()
//#endif
));
}
// Gets the player's combined inventory; their inventory, plus offhand and armor.
@Nullable
private ItemStack @NotNull [] getCombinedInventory(@NotNull PlayerInventory inv) {
final ItemStack[] combined = new ItemStack[inv.main.size() + inv.armor.size() + inv.offHand.size()];
System.arraycopy(
inv.main.toArray(new ItemStack[0]), 0, combined,
0, inv.main.size()
);
System.arraycopy(
inv.armor.toArray(new ItemStack[0]), 0, combined,
inv.main.size(), inv.armor.size()
);
System.arraycopy(
inv.offHand.toArray(new ItemStack[0]), 0, combined,
inv.main.size() + inv.armor.size(), inv.offHand.size()
);
//#if MC<12105
//$$ final ItemStack[] combined = new ItemStack[inv.main.size() + inv.armor.size() + inv.offHand.size()];
//$$ System.arraycopy(
//$$ inv.main.toArray(new ItemStack[0]), 0, combined,
//$$ 0, inv.main.size()
//$$ );
//$$ System.arraycopy(
//$$ inv.armor.toArray(new ItemStack[0]), 0, combined,
//$$ inv.main.size(), inv.armor.size()
//$$ );
//$$ System.arraycopy(
//$$ inv.offHand.toArray(new ItemStack[0]), 0, combined,
//$$ inv.main.size() + inv.armor.size(), inv.offHand.size()
//$$ );
//$$ return combined;
//#else
final ItemStack[] combined = new ItemStack[inv.size()];
int slot = 0;
while (inv.iterator().hasNext()) {
combined[slot] = inv.iterator().next();
slot++;
}
return combined;
//#endif
}
@NotNull

View File

@@ -71,7 +71,11 @@ public abstract class ServerPlayNetworkHandlerMixin {
@Inject(method = "onClickSlot", at = @At("HEAD"), cancellable = true)
public void onClickSlot(ClickSlotC2SPacket packet, CallbackInfo ci) {
int slot = packet.getSlot();
//#if MC<12105
//$$ int slot = packet.getSlot();
//#else
int slot = packet.slot();
//#endif
if (slot < 0) {
return;
}