Changed dependency

Trinkets is now an optional dependency
This commit is contained in:
Konhaiii 2025-06-04 06:13:04 +02:00
parent 51d83fabaf
commit e28e417201
8 changed files with 95 additions and 41 deletions

View file

@ -1,14 +1,17 @@
package konhaiii.powered_jetpacks.hud;
import dev.emi.trinkets.api.TrinketsApi;
import konhaiii.powered_jetpacks.PoweredJetpacks;
import konhaiii.powered_jetpacks.item.special.JetpackItem;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Pair;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static net.minecraft.client.resource.language.I18n.translate;
@ -20,13 +23,18 @@ public class JetpackHUD implements HudRenderCallback {
if (client.player == null) return;
ItemStack chestStack = client.player.getEquippedStack(EquipmentSlot.CHEST);
ItemStack backStack = TrinketsApi.getTrinketComponent(client.player).map(component ->
component.getEquipped(stack -> stack.getItem() instanceof JetpackItem)
.stream()
.findFirst()
.map(Pair::getRight)
.orElse(ItemStack.EMPTY)
).orElse(ItemStack.EMPTY);
ItemStack backStack = ItemStack.EMPTY;
if (PoweredJetpacks.isTrinketsLoaded) {
try {
Class<?> optionalClass = Class.forName("konhaiii.powered_jetpacks.compat.TrinketsServer");
Method getBackStackMethod = optionalClass.getMethod("getBackStack", LivingEntity.class);
backStack = (ItemStack) getBackStackMethod.invoke(null, client.player);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException e) {
PoweredJetpacks.LOGGER.error("Could not load Trinkets compat class.");
}
}
ItemStack jetpackStack = isValidJetpack(chestStack) ? chestStack : (!backStack.isEmpty() ? backStack : ItemStack.EMPTY);

View file

@ -1,15 +1,15 @@
package konhaiii.powered_jetpacks.mixin.client;
import dev.emi.trinkets.api.TrinketsApi;
import konhaiii.powered_jetpacks.PoweredJetpacks;
import konhaiii.powered_jetpacks.item.special.JetpackItem;
import konhaiii.powered_jetpacks.packet.JetpackPacket;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Pair;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
@ -17,6 +17,9 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Mixin(ClientPlayerEntity.class)
public abstract class ClientPlayerEntityMixin {
@Unique
@ -26,13 +29,19 @@ public abstract class ClientPlayerEntityMixin {
private void onTick(CallbackInfo ci) {
ClientPlayerEntity player = (ClientPlayerEntity) (Object) this;
ItemStack chestStack = player.getEquippedStack(EquipmentSlot.CHEST);
ItemStack backStack = TrinketsApi.getTrinketComponent(player).map(component ->
component.getEquipped(stack -> stack.getItem() instanceof JetpackItem)
.stream()
.findFirst()
.map(Pair::getRight)
.orElse(ItemStack.EMPTY)
).orElse(ItemStack.EMPTY);
ItemStack backStack = ItemStack.EMPTY;
if (PoweredJetpacks.isTrinketsLoaded) {
try {
Class<?> optionalClass = Class.forName("konhaiii.powered_jetpacks.compat.TrinketsServer");
Method getBackStackMethod = optionalClass.getMethod("getBackStack", LivingEntity.class);
backStack = (ItemStack) getBackStackMethod.invoke(null, player);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException e) {
PoweredJetpacks.LOGGER.error("Could not load Trinkets compat class.");
}
}
ItemStack jetpackStack = null;
if (isValidJetpack(chestStack)) {
jetpackStack = chestStack;

View file

@ -1,6 +1,5 @@
package konhaiii.powered_jetpacks.renderers;
import dev.emi.trinkets.api.TrinketsApi;
import konhaiii.powered_jetpacks.PoweredJetpacks;
import konhaiii.powered_jetpacks.item.ModItems;
import konhaiii.powered_jetpacks.item.special.JetpackItem;
@ -17,9 +16,11 @@ import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.math.RotationAxis;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class JetpackRenderer<T extends LivingEntity, M extends EntityModel<T>> extends FeatureRenderer<T, M> {
private final JetpackModel<T> jetpackModel;
private static final Identifier BASIC_JETPACK_TEXTURE = new Identifier(PoweredJetpacks.MOD_ID, "textures/jetpack/basic_jetpack.png");
@ -34,14 +35,17 @@ public class JetpackRenderer<T extends LivingEntity, M extends EntityModel<T>> e
@Override
public void render(MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int light, T entity, float limbAngle, float limbDistance, float tickDelta, float animationProgress, float headYaw, float headPitch) {
ItemStack chestStack = entity.getEquippedStack(EquipmentSlot.CHEST);
ItemStack backStack = TrinketsApi.getTrinketComponent(entity).map(component ->
component.getEquipped(stack -> stack.getItem() instanceof JetpackItem)
.stream()
.findFirst()
.map(Pair::getRight)
.orElse(ItemStack.EMPTY)
).orElse(ItemStack.EMPTY);
ItemStack backStack = ItemStack.EMPTY;
if (PoweredJetpacks.isTrinketsLoaded) {
try {
Class<?> optionalClass = Class.forName("konhaiii.powered_jetpacks.compat.TrinketsClient");
Method getBackStackMethod = optionalClass.getMethod("getBackStackLivingEntity", LivingEntity.class);
backStack = (ItemStack) getBackStackMethod.invoke(null, entity);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException |
IllegalAccessException e) {
PoweredJetpacks.LOGGER.error("Could not load Trinkets compat class.");
}
}
ItemStack jetpackStack = isValidJetpack(chestStack) ? chestStack : (!backStack.isEmpty() ? backStack : ItemStack.EMPTY);