Skip to content

Commit

Permalink
Fix resource pack building, don't bundle server translations api
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 20, 2021
1 parent 16a17c5 commit 576a308
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 28 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
}
```

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 14 additions & 19 deletions src/main/java/eu/pb4/polymer/item/ItemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
41 changes: 37 additions & 4 deletions src/main/java/eu/pb4/polymer/resourcepack/DefaultRPBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/polymer.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"other.SynchronizeRecipesS2CPacketMixin",
"polymc.BlockPolyGeneratorMixin"
],
"client": [
"item.EntityEquipmentUpdateS2CPacketMixin",
"item.InventoryS2CPacketMixin",
"item.ScreenHandlerSlotUpdateS2CPacketMixin"
],
"injectors": {
"defaultRequire": 1
}
Expand Down
2 changes: 2 additions & 0 deletions src/testmod/java/eu/pb4/polymertest/TestMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 576a308

Please sign in to comment.