9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-20 07:19:16 +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.stat.Stats;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.world.GameMode;
import net.william278.desertwell.util.ThrowingConsumer; import net.william278.desertwell.util.ThrowingConsumer;
import net.william278.husksync.FabricHuskSync; import net.william278.husksync.FabricHuskSync;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
@@ -188,7 +189,11 @@ public abstract class FabricData implements Data {
for (int slot = 0; slot < player.getInventory().size(); slot++) { for (int slot = 0; slot < player.getInventory().size(); slot++) {
player.getInventory().setStack(slot, items[slot] == null ? ItemStack.EMPTY : items[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.playerScreenHandler.sendContentUpdates();
player.getInventory().updateItems(); player.getInventory().updateItems();
} }
@@ -888,7 +893,11 @@ public abstract class FabricData implements Data {
@Override @Override
public void apply(@NotNull FabricUser user, @NotNull FabricHuskSync plugin) throws IllegalStateException { 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 FabricHuskSync plugin = (FabricHuskSync) getPlugin();
final NbtCompound root; final NbtCompound root;
try { try {
root = StringNbtReader.parse(serialized); //#if MC<12105
//$$ root = StringNbtReader.parse(serialized);
//#else
root = StringNbtReader.readCompound(serialized);
//#endif
} catch (Throwable e) { } catch (Throwable e) {
throw new DeserializationException("Failed to read item NBT from string (%s)".formatted(serialized), e); throw new DeserializationException("Failed to read item NBT from string (%s)".formatted(serialized), e);
} }
// Deserialize the inventory data // 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( return FabricData.Items.Inventory.from(
items != null ? getItems(items, dataMcVersion, plugin) : new ItemStack[INVENTORY_SLOT_COUNT], items != null ? getItems(items, dataMcVersion, plugin) : new ItemStack[INVENTORY_SLOT_COUNT],
root.contains(HELD_ITEM_SLOT_TAG) ? root.getInt(HELD_ITEM_SLOT_TAG) : 0 root.getInt(HELD_ITEM_SLOT_TAG, 0)
); );
//#endif
} }
@Override @Override
@@ -121,7 +133,11 @@ public abstract class FabricSerializer {
throws DeserializationException { throws DeserializationException {
final FabricHuskSync plugin = (FabricHuskSync) getPlugin(); final FabricHuskSync plugin = (FabricHuskSync) getPlugin();
try { 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)); return FabricData.Items.EnderChest.adapt(getItems(items, dataMcVersion, plugin));
} catch (Throwable e) { } catch (Throwable e) {
throw new DeserializationException("Failed to read item NBT from string (%s)".formatted(serialized), 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); 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(); 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 -> { itemList.forEach(element -> {
final NbtCompound compound = (NbtCompound) 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; return contents;
} catch (Throwable e) { } catch (Throwable e) {
throw new Serializer.DeserializationException("Failed to read item NBT string (%s)".formatted(tag), e); throw new Serializer.DeserializationException("Failed to read item NBT string (%s)".formatted(tag), e);
@@ -199,19 +227,37 @@ public abstract class FabricSerializer {
@NotNull @NotNull
private ItemStack @NotNull [] upgradeItemStacks(@NotNull NbtCompound items, @NotNull Version mcVersion, private ItemStack @NotNull [] upgradeItemStacks(@NotNull NbtCompound items, @NotNull Version mcVersion,
@NotNull FabricHuskSync plugin) { @NotNull FabricHuskSync plugin) {
final int size = items.getInt("size"); //#if MC<12105
final NbtList list = items.getList("items", NbtElement.COMPOUND_TYPE); //$$ 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 ItemStack[] itemStacks = new ItemStack[size];
final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager(); final DynamicRegistryManager registryManager = plugin.getMinecraftServer().getRegistryManager();
Arrays.fill(itemStacks, ItemStack.EMPTY); Arrays.fill(itemStacks, ItemStack.EMPTY);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
if (list.getCompound(i) == null) { final NbtCompound compound = list.getCompoundOrEmpty(i);
if (compound.isEmpty()) {
continue; continue;
} }
final NbtCompound compound = list.getCompound(i); final int slot = compound.getInt("Slot", -1);
final int slot = compound.getInt("Slot"); if (slot >= 0) {
itemStacks[slot] = decodeNbt(upgradeItemData(list.getCompound(i), mcVersion, plugin), registryManager); itemStacks[slot] = decodeNbt(upgradeItemData(compound, mcVersion, plugin), registryManager);
}
} }
//#endif
return itemStacks; return itemStacks;
} }

View File

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

View File

@@ -71,7 +71,11 @@ public abstract class ServerPlayNetworkHandlerMixin {
@Inject(method = "onClickSlot", at = @At("HEAD"), cancellable = true) @Inject(method = "onClickSlot", at = @At("HEAD"), cancellable = true)
public void onClickSlot(ClickSlotC2SPacket packet, CallbackInfo ci) { 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) { if (slot < 0) {
return; return;
} }