Skip to content

Commit

Permalink
Slight extension to resourcepack api
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed May 25, 2024
1 parent f869dbc commit f1a3ab4
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.97.6+1.20.6

maven_group = eu.pb4

mod_version = 0.8.1-dev
mod_version = 0.8.1

minecraft_version_supported = ">=1.20.5-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class InternalEntityHelpers {
private static final Map<EntityType<?>, @Nullable Entity> EXAMPLE_ENTITIES = new HashMap<>();
private static final Map<EntityType<?>, DataTracker.Entry<?>[]> TRACKED_DATA = new Object2ObjectOpenCustomHashMap<>(CommonImplUtils.IDENTITY_HASH);

static {
private static PlayerEntity createPlayer() {
PlayerEntity player = null;
try {
EXAMPLE_ENTITIES.put(EntityType.PLAYER, new PlayerEntity(FakeWorld.INSTANCE_UNSAFE, BlockPos.ORIGIN, 0, new GameProfile(Util.NIL_UUID, "TinyPotato")) {
player = new PlayerEntity(FakeWorld.INSTANCE_UNSAFE, BlockPos.ORIGIN, 0, new GameProfile(Util.NIL_UUID, "TinyPotato")) {
@Override
public boolean isSpectator() {
return false;
Expand All @@ -43,13 +44,13 @@ public boolean isSpectator() {
public boolean isCreative() {
return false;
}
});
};
} catch (Throwable e) {
if (CommonImpl.LOG_MORE_ERRORS) {
CommonImpl.LOGGER.error("Failed add player like entity! Trying with alternative method", e);
}
try {
EXAMPLE_ENTITIES.put(EntityType.PLAYER, new PlayerEntity(FakeWorld.INSTANCE_REGULAR, BlockPos.ORIGIN, 0, new GameProfile(Util.NIL_UUID, "TinyPotato")) {
player = new PlayerEntity(FakeWorld.INSTANCE_REGULAR, BlockPos.ORIGIN, 0, new GameProfile(Util.NIL_UUID, "TinyPotato")) {
@Override
public boolean isSpectator() {
return false;
Expand All @@ -59,14 +60,16 @@ public boolean isSpectator() {
public boolean isCreative() {
return false;
}
});
};
} catch (Throwable e2) {
if (CommonImpl.LOG_MORE_ERRORS) {
CommonImpl.LOGGER.error("Failed add player like entity!", e2);
}
}
}
}
EXAMPLE_ENTITIES.put(EntityType.PLAYER, player);
return player;
};

public static DataTracker.Entry<?>[] getExampleTrackedDataOfEntityType(EntityType<?> type) {
var val = TRACKED_DATA.get(type);
Expand Down Expand Up @@ -107,6 +110,10 @@ public static Entity getEntity(EntityType<?> type) {
Entity entity = EXAMPLE_ENTITIES.get(type);

if (entity == null) {
if (type == EntityType.PLAYER) {
return createPlayer();
}

try {
entity = type.create(FakeWorld.INSTANCE_UNSAFE);
} catch (Throwable e) {
Expand All @@ -118,10 +125,10 @@ public static Entity getEntity(EntityType<?> type) {
CommonImpl.LOGGER.warn(String.format(
"Couldn't create template entity of %s... Defaulting to empty. %s",
id,
id != null && id.getNamespace().equals("minecraft") ? "This might cause problems!" : "Don't worry, this shouldn't cause problems!"
id.getNamespace().equals("minecraft") ? "This might cause problems!" : "Don't worry, this shouldn't cause problems!"
));

if (id != null && id.getNamespace().equals("minecraft")) {
if (id.getNamespace().equals("minecraft")) {
CommonImpl.LOGGER.warn("First error:");
e.printStackTrace();
CommonImpl.LOGGER.warn("Second error:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static boolean areSamePolymerType(ItemStack a, ItemStack b) {
return Objects.equals(getItemId(a.getItem(), a.get(DataComponentTypes.CUSTOM_DATA)), getItemId(b.getItem(), b.get(DataComponentTypes.CUSTOM_DATA)));
}

public static boolean areSamePolymerType(Item ai, NbtComponent a, Item bi, NbtComponent b) {
public static boolean areSamePolymerType(Object ai, NbtComponent a, Object bi, NbtComponent b) {
return Objects.equals(getItemId(ai, a), getItemId(bi, b));
}

Expand Down Expand Up @@ -66,11 +66,11 @@ public static Object getKey(@Nullable NbtComponent component) {
return Registries.ITEM.get(id);
}

private static Identifier getItemId(Item item, @Nullable NbtComponent nbtComponent) {
private static Identifier getItemId(Object item, @Nullable NbtComponent nbtComponent) {
var id = PolymerItemUtils.getServerIdentifier(nbtComponent);

if (id == null) {
return item.getRegistryEntry().registryKey().getValue();
if (id == null && item instanceof Item item1) {
return item1.getRegistryEntry().registryKey().getValue();
}

return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import eu.pb4.polymer.core.impl.client.compat.CompatUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.DataComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -21,16 +25,22 @@ public abstract class emi_EmiStackMixin {

@Shadow public abstract ItemStack getItemStack();

@Shadow public abstract Object getKey();

@Shadow public abstract <T> @Nullable T get(DataComponentType<? extends T> type);

@Inject(method = "isEqual(Ldev/emi/emi/api/stack/EmiStack;)Z", at = @At("HEAD"), cancellable = true, remap = false, require = 0)
private void polymer$areEqual(EmiStack stack, CallbackInfoReturnable<Boolean> cir) {
if (!CompatUtils.areSamePolymerType(stack.getItemStack(), this.getItemStack())) {
if (!CompatUtils.areSamePolymerType(null, stack.get(DataComponentTypes.CUSTOM_DATA),
null, this.get(DataComponentTypes.CUSTOM_DATA))) {
cir.setReturnValue(false);
}
}

@Inject(method = "isEqual(Ldev/emi/emi/api/stack/EmiStack;Ldev/emi/emi/api/stack/Comparison;)Z", at = @At("HEAD"), cancellable = true, remap = false, require = 0)
private void polymer$areEqual2(EmiStack stack, Comparison comparison, CallbackInfoReturnable<Boolean> cir) {
if (!CompatUtils.areSamePolymerType(stack.getItemStack(), this.getItemStack())) {
if (!CompatUtils.areSamePolymerType(null, stack.get(DataComponentTypes.CUSTOM_DATA),
null, this.get(DataComponentTypes.CUSTOM_DATA))) {
cir.setReturnValue(false);
}
}
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion polymer-core/src/main/resources/polymer-core.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"client.item.ItemGroupMixin",
"client.item.ItemGroupsMixin",
"client.item.ItemStackMixin",
"client.rendering.AbstractInventoryScreenMixin",
"client.rendering.BlockModelsMixin",
"client.rendering.BuiltinModelItemRendererMixin",
"client.rendering.EntityRenderDispatcherMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.entity.Entity;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket;
import net.minecraft.network.packet.c2s.play.PlayerInputC2SPacket;
import net.minecraft.network.packet.s2c.play.EntityPassengersSetS2CPacket;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -44,4 +45,9 @@ private void sendPassengers(CallbackInfo ci) {
this.lastPassengers = list;
}
}

@Inject(method = "onHandSwing", at = @At("TAIL"))
private void onSwing(HandSwingC2SPacket packet, CallbackInfo ci) {
//this.player.sendMessage(Text.literal("Swing!"));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package eu.pb4.polymer.resourcepack.api;

import net.minecraft.item.ArmorMaterial;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;

/**
* Represents information about armor texture
*/
@ApiStatus.NonExtendable
public interface PolymerArmorModel{
public interface PolymerArmorModel {
int color();
Identifier modelPath();
Identifier modelId();
List<ArmorMaterial.Layer> layers();
@Deprecated
default Identifier modelPath() {
return modelId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import eu.pb4.polymer.resourcepack.impl.compat.polymc.PolyMcHelpers;
import eu.pb4.polymer.resourcepack.impl.generation.DefaultRPBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.Item;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -55,6 +57,15 @@ public static PolymerModelData requestModel(Item vanillaItem, Identifier modelPa
public static PolymerArmorModel requestArmor(Identifier modelPath) {
return INSTANCE.requestArmor(modelPath);
}
/**
* This method can be used to register custom model data for items
*
* @param material ArmorMaterial to generate
* @return PolymerArmorModel with data about this model
*/
public static PolymerArmorModel requestArmor(RegistryEntry<ArmorMaterial> material) {
return INSTANCE.requestArmor(material);
}

/**
* Adds mod with provided mod id as a source of assets
Expand All @@ -66,7 +77,7 @@ public static boolean addModAssets(String modId) {
}

/**
* Adds mod with provided mod id as a source of assets
* Adds mod with provided mod id as a source of assets, without actually copying them to the resource pack
*
* @param modId Id of mods used as a source
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import eu.pb4.polymer.resourcepack.impl.generation.DefaultRPBuilder;
import eu.pb4.polymer.resourcepack.impl.generation.PolymerArmorModelImpl;
import eu.pb4.polymer.resourcepack.impl.generation.PolymerModelDataImpl;
import eu.pb4.polymer.resourcepack.mixin.LayerAccessor;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.SharedConstants;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.Item;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.Text;
import net.minecraft.text.TextCodecs;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -49,7 +52,7 @@ public final class ResourcePackCreator {
private int armorColor = 0;
private Text packDescription = null;
private byte[] packIcon = null;
private Set<Path> sourcePaths = new HashSet<>();
private final Set<Path> sourcePaths = new HashSet<>();

public static ResourcePackCreator create() {
return new ResourcePackCreator(0);
Expand Down Expand Up @@ -122,13 +125,28 @@ public PolymerArmorModel requestArmor(Identifier modelPath) {
} else {
this.armorColor++;
int color = 0xFFFFFF - armorColor * 2 + 1;
var model = new PolymerArmorModelImpl(color, modelPath);
var model = new PolymerArmorModelImpl(color, modelPath, List.of(new ArmorMaterial.Layer(modelPath)));

this.armorModelMap.put(modelPath, model);
this.takenArmorColors.add(color);
return model;
}
}
/**
* This method can be used to register custom model data for items
*
* @param material ArmorMaterial
* @return PolymerArmorModel with data about this model
*/
public PolymerArmorModel requestArmor(RegistryEntry<ArmorMaterial> material) {
if (material.value().layers().isEmpty()) {
CommonImpl.LOGGER.warn("Armor Material '{}' has no layers!", material.getIdAsString());
return PolymerArmorModelImpl.EMPTY;
} else if (material.value().layers().size() > 1) {
CommonImpl.LOGGER.warn("Multi-layer Armor Materials aren't supported yet! Provided material: '{}'", material.getIdAsString());
}
return requestArmor(((LayerAccessor) (Object) material.value().layers().get(0)).getId());
}

/**
* Adds mod with provided mod id as a source of assets
Expand Down Expand Up @@ -181,6 +199,26 @@ public List<PolymerModelData> getModelsFor(Item item) {
return Collections.unmodifiableList(items.getOrDefault(item, Collections.emptyList()));
}

/**
* Gets an unmodifiable list of models for all items
* This can be useful if you need to extract this list and parse it yourself.
*
* @return An unmodifiable list of models
*/
public Map<Item, List<PolymerModelData>> getAllItemModels() {
return Collections.unmodifiableMap(items);
}

/**
* Gets an unmodifiable list of models for all items
* This can be useful if you need to extract this list and parse it yourself.
*
* @return An unmodifiable list of models
*/
public Map<Identifier, PolymerArmorModel> getAllArmorModels() {
return Collections.unmodifiableMap(this.armorModelMap);
}

/**
* Sets pack description
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package eu.pb4.polymer.resourcepack.impl.generation;

import eu.pb4.polymer.resourcepack.api.PolymerArmorModel;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.util.Identifier;

public record PolymerArmorModelImpl(int color, Identifier modelPath) implements PolymerArmorModel {
import java.util.List;

public record PolymerArmorModelImpl(int color, Identifier modelId, List<ArmorMaterial.Layer> layers) implements PolymerArmorModel {
public static final PolymerArmorModel EMPTY = new PolymerArmorModelImpl(-1, new Identifier("empty"), List.of());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.pb4.polymer.resourcepack.mixin;

import net.minecraft.item.ArmorMaterial;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ArmorMaterial.Layer.class)
public interface LayerAccessor {
@Accessor
Identifier getId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"compatibilityLevel": "JAVA_21",
"plugin": "eu.pb4.polymer.resourcepack.mixin.PolymerResourcePackMixinConfigPlugin",
"mixins": [
"LayerAccessor",
"compat.polymc.polymc_ArmorColorManagerMixin",
"compat.polymc.polymc_FancyPantsItemPolyMixin"
],
"client": [
"client.MinecraftClientMixin",
"client.ArmorFeatureRendererAccessor",
"client.MinecraftClientMixin",
"client.ResourcePackManagerMixin",
"client.compat.armor_ArmorFeatureRendererMixin"
],
Expand Down

0 comments on commit f1a3ab4

Please sign in to comment.