From 576a308ea7e7b29271a1e08c17fdb1ad8328994d Mon Sep 17 00:00:00 2001
From: Patbox <39821509+Patbox@users.noreply.github.com>
Date: Sun, 20 Jun 2021 08:34:42 +0200
Subject: [PATCH] Fix resource pack building, don't bundle server translations
 api

---
 README.md                                     |  9 +++-
 build.gradle                                  |  3 +-
 gradle.properties                             |  2 +-
 .../java/eu/pb4/polymer/item/ItemHelper.java  | 33 +++++++--------
 .../EntityTrackerUpdateS2CPacketMixin.java    | 25 ++++++++++-
 .../EntityEquipmentUpdateS2CPacketMixin.java  | 36 ++++++++++++++++
 .../mixin/item/InventoryS2CPacketMixin.java   | 33 +++++++++++++++
 ...ScreenHandlerSlotUpdateS2CPacketMixin.java | 25 +++++++++++
 .../resourcepack/DefaultRPBuilder.java        | 41 +++++++++++++++++--
 src/main/resources/polymer.mixins.json        |  5 +++
 .../java/eu/pb4/polymertest/TestMod.java      |  2 +
 11 files changed, 186 insertions(+), 28 deletions(-)
 create mode 100644 src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java
 create mode 100644 src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java
 create mode 100644 src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java

diff --git a/README.md b/README.md
index 808ebac7..714a8fb3 100644
--- a/README.md
+++ b/README.md
@@ -6,13 +6,18 @@ This library is its alpha stages, however it should be stable. Feel free to sugg
 ## Usage:
 Add it to your dependencies like this:
 
-```
+```groovy
 repositories {
 	maven { url 'https://maven.nucleoid.xyz' }
 }
 
 dependencies {
-	modImplementation include("eu.pb4.polymer:[TAG]").
+	modImplementation include("eu.pb4.polymer:[TAG]")
+	
+	// Optional, but highly recommended!
+	// Allows to create server side translations with vanilla like way
+    // You can get version tag at https://maven.nucleoid.xyz/fr/catcore/server-translations-api/
+	modImplementation include("fr.catcore:server-translations-api:[STA-TAG]")
 }
 ```
 
diff --git a/build.gradle b/build.gradle
index 0678bb6b..8d03f6e5 100644
--- a/build.gradle
+++ b/build.gradle
@@ -65,9 +65,10 @@ dependencies {
 	modCompileOnly 'com.github.TheEpicBlock:PolyMc:ebe3100f5ad7'
 	//modRuntime 'com.github.TheEpicBlock:PolyMc:ebe3100f5ad7'
 
-	modImplementation include("fr.catcore:server-translations-api:1.4.4+1.17")
 	modImplementation include("xyz.nucleoid:packet-tweaker:0.2.0-1.17-pre1")
 
+	modRuntime "fr.catcore:server-translations-api:1.4.4+1.17"
+
 	// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
 	// You may need to force-disable transitiveness on them.
 	testmodImplementation sourceSets.main.output
diff --git a/gradle.properties b/gradle.properties
index 2778842e..ae2050fa 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
 	loader_version=0.11.3
 
 # Mod Properties
-	mod_version = 0.1.0-pre2-1.17
+	mod_version = 0.1.0-pre3-1.17
 	maven_group = eu.pb4
 	archives_base_name = polymer
 
diff --git a/src/main/java/eu/pb4/polymer/item/ItemHelper.java b/src/main/java/eu/pb4/polymer/item/ItemHelper.java
index 5f548d2e..db150c82 100644
--- a/src/main/java/eu/pb4/polymer/item/ItemHelper.java
+++ b/src/main/java/eu/pb4/polymer/item/ItemHelper.java
@@ -15,7 +15,9 @@
 import net.minecraft.nbt.NbtList;
 import net.minecraft.nbt.NbtString;
 import net.minecraft.server.network.ServerPlayerEntity;
-import net.minecraft.text.*;
+import net.minecraft.text.LiteralText;
+import net.minecraft.text.Style;
+import net.minecraft.text.Text;
 import net.minecraft.util.Formatting;
 import net.minecraft.util.Identifier;
 import net.minecraft.util.registry.Registry;
@@ -107,12 +109,6 @@ public static ItemStack createBasicVirtualItemStack(ItemStack itemStack, @Nullab
             out.getOrCreateTag().put(ItemHelper.REAL_TAG, itemStack.getTag());
             assert out.getTag() != null;
 
-            if (!itemStack.hasCustomName() && (player == null || itemStack.getFrame() == null)) {
-                out.setCustomName(itemStack.getItem().getName(itemStack).shallowCopy().fillStyle(ItemHelper.NON_ITALIC_STYLE.withColor(itemStack.getRarity().formatting)));
-            } else {
-                out.setCustomName(itemStack.getName());
-            }
-
             int dmg = itemStack.getDamage();
             if (dmg != 0) {
                 out.getTag().putInt("Damage", (int) ((((double) dmg) / itemStack.getItem().getMaxDamage()) * item.getMaxDamage()));
@@ -144,22 +140,21 @@ public static ItemStack createBasicVirtualItemStack(ItemStack itemStack, @Nullab
             }
         }
 
-        if (player == null || itemStack.getFrame() == null) {
-            List<Text> tooltip = itemStack.getTooltip(player, TooltipContext.Default.NORMAL);
-            tooltip.remove(0);
+        List<Text> tooltip = itemStack.getTooltip(player, TooltipContext.Default.NORMAL);
+        out.setCustomName(tooltip.remove(0));
 
-            if (itemStack.getItem() instanceof VirtualItem) {
-                ((VirtualItem) itemStack.getItem()).modifyTooltip(tooltip, itemStack, player);
-            }
+        if (itemStack.getItem() instanceof VirtualItem) {
+            ((VirtualItem) itemStack.getItem()).modifyTooltip(tooltip, itemStack, player);
+        }
 
-            for (Text t : tooltip) {
-                lore.add(NbtString.of(Text.Serializer.toJson(new LiteralText("").append(t).setStyle(ItemHelper.CLEAN_STYLE))));
-            }
+        for (Text t : tooltip) {
+            lore.add(NbtString.of(Text.Serializer.toJson(new LiteralText("").append(t).setStyle(ItemHelper.CLEAN_STYLE))));
+        }
 
-            if (lore.size() > 0) {
-                out.getOrCreateTag().getCompound("display").put("Lore", lore);
-            }
+        if (lore.size() > 0) {
+            out.getOrCreateTag().getCompound("display").put("Lore", lore);
         }
+
         return out;
     }
 }
diff --git a/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java
index a748ba3c..8d08b197 100644
--- a/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java
+++ b/src/main/java/eu/pb4/polymer/mixin/entity/EntityTrackerUpdateS2CPacketMixin.java
@@ -1,16 +1,23 @@
 package eu.pb4.polymer.mixin.entity;
 
 import eu.pb4.polymer.entity.VirtualEntity;
+import eu.pb4.polymer.item.ItemHelper;
 import eu.pb4.polymer.other.Helpers;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.MinecraftClient;
 import net.minecraft.entity.Entity;
 import net.minecraft.entity.data.DataTracker;
+import net.minecraft.item.ItemStack;
 import net.minecraft.network.packet.s2c.play.EntityTrackerUpdateS2CPacket;
+import net.minecraft.server.network.ServerPlayerEntity;
 import org.spongepowered.asm.mixin.Mixin;
 import org.spongepowered.asm.mixin.Mutable;
 import org.spongepowered.asm.mixin.Shadow;
 import org.spongepowered.asm.mixin.injection.At;
 import org.spongepowered.asm.mixin.injection.Inject;
 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -53,4 +60,20 @@ private void removeInvalidEntries(int id, DataTracker tracker, boolean forceUpda
             }
         }
     }
-}
+
+    @Environment(EnvType.CLIENT)
+    @Inject(method = "getTrackedValues", at = @At("RETURN"), cancellable = true)
+    private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<DataTracker.Entry<?>>> cir) {
+        List<DataTracker.Entry<?>> list = new ArrayList<>();
+        ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
+
+        for (DataTracker.Entry<?> entry : cir.getReturnValue()) {
+            if (entry.get() instanceof ItemStack stack) {
+                list.add(new DataTracker.Entry(entry.getData(), ItemHelper.getVirtualItemStack(stack, player)));
+            } else {
+                list.add(entry);
+            }
+        }
+
+        cir.setReturnValue(list);
+    }}
diff --git a/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java
new file mode 100644
index 00000000..3d8d025a
--- /dev/null
+++ b/src/main/java/eu/pb4/polymer/mixin/item/EntityEquipmentUpdateS2CPacketMixin.java
@@ -0,0 +1,36 @@
+package eu.pb4.polymer.mixin.item;
+
+import com.mojang.datafixers.util.Pair;
+import eu.pb4.polymer.item.ItemHelper;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.entity.EquipmentSlot;
+import net.minecraft.item.ItemStack;
+import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
+import net.minecraft.server.network.ServerPlayerEntity;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+import xyz.nucleoid.packettweaker.PacketContext;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Mixin(EntityEquipmentUpdateS2CPacket.class)
+public class EntityEquipmentUpdateS2CPacketMixin {
+    @Environment(EnvType.CLIENT)
+    @Inject(method = "getEquipmentList", at = @At("RETURN"), cancellable = true)
+    private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<Pair<EquipmentSlot, ItemStack>>> cir) {
+        List<Pair<EquipmentSlot, ItemStack>> list = new ArrayList<>();
+        ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
+
+        for (Pair<EquipmentSlot, ItemStack> pair : cir.getReturnValue()) {
+            list.add(new Pair<>(pair.getFirst(), ItemHelper.getVirtualItemStack(pair.getSecond(), player)));
+        }
+
+        cir.setReturnValue(list);
+    }
+
+}
diff --git a/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java
new file mode 100644
index 00000000..8df375be
--- /dev/null
+++ b/src/main/java/eu/pb4/polymer/mixin/item/InventoryS2CPacketMixin.java
@@ -0,0 +1,33 @@
+package eu.pb4.polymer.mixin.item;
+
+import eu.pb4.polymer.item.ItemHelper;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.item.ItemStack;
+import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
+import net.minecraft.server.network.ServerPlayerEntity;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+@Mixin(InventoryS2CPacket.class)
+public class InventoryS2CPacketMixin {
+    @Environment(EnvType.CLIENT)
+    @Inject(method = "getContents", at = @At("RETURN"), cancellable = true)
+    private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<List<ItemStack>> cir) {
+        List<ItemStack> list = new ArrayList<>();
+        ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
+
+        for (ItemStack stack : cir.getReturnValue()) {
+            list.add(ItemHelper.getVirtualItemStack(stack, player));
+        }
+
+        cir.setReturnValue(list);
+    }
+}
diff --git a/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java b/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java
new file mode 100644
index 00000000..1dbdefa1
--- /dev/null
+++ b/src/main/java/eu/pb4/polymer/mixin/item/ScreenHandlerSlotUpdateS2CPacketMixin.java
@@ -0,0 +1,25 @@
+package eu.pb4.polymer.mixin.item;
+
+import eu.pb4.polymer.item.ItemHelper;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.item.ItemStack;
+import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
+import net.minecraft.server.network.ServerPlayerEntity;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+import xyz.nucleoid.packettweaker.PacketContext;
+
+
+@Mixin(ScreenHandlerSlotUpdateS2CPacket.class)
+public class ScreenHandlerSlotUpdateS2CPacketMixin {
+    @Environment(EnvType.CLIENT)
+    @Inject(method = "getItemStack", at = @At("RETURN"), cancellable = true)
+    private void replaceItemsWithVirtualOnes(CallbackInfoReturnable<ItemStack> cir) {
+        ServerPlayerEntity player = MinecraftClient.getInstance().getServer().getPlayerManager().getPlayer(MinecraftClient.getInstance().player.getUuid());
+        cir.setReturnValue(ItemHelper.getVirtualItemStack(cir.getReturnValue(), player));
+    }
+}
diff --git a/src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java b/src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java
index fd345856..5a0e583a 100644
--- a/src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java
+++ b/src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java
@@ -13,13 +13,14 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
@@ -81,7 +82,39 @@ public boolean copyModAssets(String modId) {
         if (mod.isPresent()) {
             ModContainer container = mod.get();
             try {
-                FileUtils.copyDirectory(container.getPath("assets").toFile(), this.outputPath.resolve("assets").toFile());
+                Path assets = container.getPath("assets");
+                Path output = this.outputPath.resolve("assets");
+                Files.walkFileTree(assets, new FileVisitor<>() {
+                    @Override
+                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                        Path fileOut;
+                        try {
+                            fileOut = output.resolve(assets.relativize(file));
+                        } catch (Exception e) {
+                            fileOut = file;
+                        }
+                        fileOut.getParent().toFile().mkdirs();
+
+                        Files.copy(file, fileOut);
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+                });
+
                 return true;
             } catch (Exception e) {
                 PolymerMod.LOGGER.error("Something went wrong while copying assets of mod: " + modId);
diff --git a/src/main/resources/polymer.mixins.json b/src/main/resources/polymer.mixins.json
index 481dbae6..7b9d20f2 100644
--- a/src/main/resources/polymer.mixins.json
+++ b/src/main/resources/polymer.mixins.json
@@ -36,6 +36,11 @@
     "other.SynchronizeRecipesS2CPacketMixin",
     "polymc.BlockPolyGeneratorMixin"
   ],
+  "client": [
+    "item.EntityEquipmentUpdateS2CPacketMixin",
+    "item.InventoryS2CPacketMixin",
+    "item.ScreenHandlerSlotUpdateS2CPacketMixin"
+  ],
   "injectors": {
     "defaultRequire": 1
   }
diff --git a/src/testmod/java/eu/pb4/polymertest/TestMod.java b/src/testmod/java/eu/pb4/polymertest/TestMod.java
index 043796bc..5850394c 100644
--- a/src/testmod/java/eu/pb4/polymertest/TestMod.java
+++ b/src/testmod/java/eu/pb4/polymertest/TestMod.java
@@ -45,6 +45,8 @@ public class TestMod implements ModInitializer {
     @Override
     public void onInitialize() {
         ResourcePackUtils.addModAsAssetsSource("polymertest");
+        //ResourcePackUtils.addModAsAssetsSource("promenade");
+
         Registry.register(Registry.ITEM, new Identifier("test", "item"), item);
         Registry.register(Registry.ITEM, new Identifier("test", "item2"), item2);
         Registry.register(Registry.BLOCK, new Identifier("test", "block"), block);