Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.17.3

# Mod Properties
mod_version = 0.3.0
mod_version = 0.4.0+1.20.6
maven_group = net.errorcraft
archives_base_name = itematic

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package net.errorcraft.itematic.mixin.client.render.item;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import net.errorcraft.itematic.item.ItemKeys;
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
import net.errorcraft.itematic.item.component.ItemComponentTypes;
import net.errorcraft.itematic.item.component.components.ShooterItemComponent;
import net.errorcraft.itematic.item.shooter.method.ShooterMethodTypes;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.item.HeldItemRenderer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -20,6 +20,7 @@
import org.spongepowered.asm.mixin.injection.Slice;

import java.util.Optional;
import java.util.OptionalInt;

@Mixin(HeldItemRenderer.class)
public class HeldItemRendererExtender {
Expand All @@ -28,24 +29,32 @@ public class HeldItemRendererExtender {
private MinecraftClient client;

@Redirect(
method = { "getHandRenderType", "getUsingItemHandRenderType" },
method = {
"getHandRenderType",
"getUsingItemHandRenderType"
},
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;isOf(Lnet/minecraft/item/Item;)Z"
),
slice = @Slice(
from = @At(
value = "FIELD",
target = "Lnet/minecraft/item/Items;BOW:Lnet/minecraft/item/Item;"
target = "Lnet/minecraft/item/Items;BOW:Lnet/minecraft/item/Item;",
opcode = Opcodes.GETSTATIC
),
to = @At(
value = "FIELD",
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;"
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;",
opcode = Opcodes.GETSTATIC
)
)
)
private static boolean isOfForBowUseRegistryKeyCheck(ItemStack instance, Item item) {
return instance.itematic$isOf(ItemKeys.BOW);
private static boolean isOfForBowUseItemComponent(ItemStack instance, Item item) {
return instance.itematic$getComponent(ItemComponentTypes.SHOOTER)
.map(ShooterItemComponent::method)
.filter(method -> method.type() == ShooterMethodTypes.DIRECT)
.isPresent();
}

@Redirect(
Expand All @@ -58,15 +67,28 @@ private static boolean isOfForBowUseRegistryKeyCheck(ItemStack instance, Item it
slice = @Slice(
from = @At(
value = "FIELD",
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;"
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;",
opcode = Opcodes.GETSTATIC
)
)
)
private boolean isOfForCrossbowUseItemComponent(ItemStack instance, Item item, @Share("shooterItemComponent") LocalRef<ShooterItemComponent> shooterItemComponent) {
Optional<ShooterItemComponent> optionalComponent = instance.itematic$getComponent(ItemComponentTypes.SHOOTER);
optionalComponent.ifPresent(shooterItemComponent::set);
return optionalComponent.map(ShooterItemComponent::isChargeable)
.orElse(false);
private boolean isOfForCrossbowUseItemComponent(ItemStack instance, Item item, AbstractClientPlayerEntity player, @Share("useDuration") LocalIntRef useDuration) {
Optional<ShooterItemComponent> optionalShooter = instance.itematic$getComponent(ItemComponentTypes.SHOOTER);
if (optionalShooter.isEmpty()) {
return false;
}

if (optionalShooter.get().method().type() != ShooterMethodTypes.CHARGEABLE) {
return false;
}

OptionalInt optionalUseDuration = optionalShooter.get().useDuration(instance, player);
if (optionalUseDuration.orElse(0) <= 0) {
return false;
}

useDuration.set(optionalUseDuration.getAsInt());
return true;
}

@Redirect(
Expand All @@ -77,69 +99,99 @@ private boolean isOfForCrossbowUseItemComponent(ItemStack instance, Item item, @
ordinal = 0
)
)
private int useDifference(AbstractClientPlayerEntity instance, @Local(argsOnly = true) ItemStack stack, @Share("shooterItemComponent") LocalRef<ShooterItemComponent> shooterItemComponent) {
return ShooterItemComponent.useDuration(stack) - instance.itematic$itemUsedTicks();
private int useDifferenceForCrossbow(AbstractClientPlayerEntity instance, @Share("useDuration") LocalIntRef useDuration) {
return useDuration.get() - instance.itematic$itemUsedTicks();
}

@Redirect(
method = "renderFirstPersonItem",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/CrossbowItem;isCharged(Lnet/minecraft/item/ItemStack;)Z"
target = "Lnet/minecraft/item/ItemStack;getMaxUseTime()I"
)
)
private boolean isChargedUseItemComponent(ItemStack stack, @Share("shooterItemComponent") LocalRef<ShooterItemComponent> shooterItemComponent) {
return shooterItemComponent.get().isCharged(stack);
private int getMaxUseTimeReturnZero(ItemStack instance) {
return 0;
}

@Redirect(
method = "renderFirstPersonItem",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;getMaxUseTime()I"
target = "Lnet/minecraft/client/network/ClientPlayerEntity;getItemUseTimeLeft()I",
ordinal = 0
),
slice = @Slice(
from = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/item/HeldItemRenderer;applyEquipOffset(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/util/Arm;F)V",
ordinal = 0
)
)
)
private int getMaxUseTimeReturnZero(ItemStack stack, AbstractClientPlayerEntity player) {
return 0;
private int getUseTimeLeftForCrossbowUseNegatedUsedTicks(ClientPlayerEntity instance) {
return -instance.itematic$itemUsedTicks();
}

@Redirect(
method = "renderFirstPersonItem",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/network/ClientPlayerEntity;getItemUseTimeLeft()I"
target = "Lnet/minecraft/client/network/AbstractClientPlayerEntity;getItemUseTimeLeft()I",
ordinal = 0
),
slice = @Slice(
from = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/item/HeldItemRenderer;renderItem(Lnet/minecraft/entity/LivingEntity;Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformationMode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V",
ordinal = 0
)
)
)
private int getUseTimeLeftUseUsedTicks(ClientPlayerEntity instance) {
return -instance.itematic$itemUsedTicks();
private int getUseTimeLeftForUseAnimationCheckUseUsedTicks(AbstractClientPlayerEntity instance) {
return instance.itematic$itemUsedTicks();
}

@Redirect(
method = "renderFirstPersonItem",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/CrossbowItem;getPullTime(Lnet/minecraft/item/ItemStack;)I"
target = "Lnet/minecraft/client/network/ClientPlayerEntity;getItemUseTimeLeft()I"
),
slice = @Slice(
from = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/item/HeldItemRenderer;applyEatOrDrinkTransformation(Lnet/minecraft/client/util/math/MatrixStack;FLnet/minecraft/util/Arm;Lnet/minecraft/item/ItemStack;)V"
)
)
)
private int getPullTimeUseItemComponent(ItemStack stack) {
return ShooterItemComponent.getPullTime(stack);
private int getUseTimeLeftForBowAndSpearUseNegatedUsedTicks(ClientPlayerEntity instance) {
return -instance.itematic$itemUsedTicks();
}

@Redirect(
method = { "getHandRenderType", "getUsingItemHandRenderType", "isChargedCrossbow" },
method = {
"getHandRenderType",
"getUsingItemHandRenderType",
"isChargedCrossbow"
},
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/item/ItemStack;isOf(Lnet/minecraft/item/Item;)Z"
),
slice = @Slice(
from = @At(
value = "FIELD",
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;"
target = "Lnet/minecraft/item/Items;CROSSBOW:Lnet/minecraft/item/Item;",
opcode = Opcodes.GETSTATIC
)
)
)
private static boolean isOfForCrossbowUseRegistryKeyCheckStatic(ItemStack instance, Item item) {
return instance.itematic$isOf(ItemKeys.CROSSBOW);
private static boolean isOfForCrossbowUseItemComponentStatic(ItemStack instance, Item item) {
return instance.itematic$getComponent(ItemComponentTypes.SHOOTER)
.map(ShooterItemComponent::method)
.filter(method -> method.type() == ShooterMethodTypes.CHARGEABLE)
.isPresent();
}

@Redirect(
Expand All @@ -152,7 +204,8 @@ private static boolean isOfForCrossbowUseRegistryKeyCheckStatic(ItemStack instan
slice = @Slice(
from = @At(
value = "FIELD",
target = "Lnet/minecraft/item/Items;FILLED_MAP:Lnet/minecraft/item/Item;"
target = "Lnet/minecraft/item/Items;FILLED_MAP:Lnet/minecraft/item/Item;",
opcode = Opcodes.GETSTATIC
)
)
)
Expand Down
24 changes: 18 additions & 6 deletions src/gametest/java/net/errorcraft/itematic/gametest/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.component.DataComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.fluid.Fluid;
Expand Down Expand Up @@ -59,25 +61,25 @@ public static void itemStackIsNotEmpty(ItemStack value) {
}
}

public static <T> void itemStackHasComponent(ItemStack value, DataComponentType<T> type) {
public static <T> void itemStackHasDataComponent(ItemStack value, DataComponentType<T> type) {
if (value.get(type) == null) {
throw new GameTestException("Expected item stack to contain the " + type + " component");
}
}

public static <T> void itemStackDoesNotHaveComponent(ItemStack value, DataComponentType<T> type) {
public static <T> void itemStackDoesNotHaveDataComponent(ItemStack value, DataComponentType<T> type) {
if (value.get(type) != null) {
throw new GameTestException("Expected item stack to not contain the " + type + " component");
}
}

public static <T> void itemStackHasComponent(ItemStack value, DataComponentType<T> type, Consumer<T> assertion) {
itemStackHasComponent(value, type);
assertion.accept(TestUtil.getComponent(value, type));
public static <T> void itemStackHasDataComponent(ItemStack value, DataComponentType<T> type, Consumer<T> assertion) {
itemStackHasDataComponent(value, type);
assertion.accept(TestUtil.getDataComponent(value, type));
}

public static void itemStackHasPotion(ItemStack value, RegistryEntry<Potion> expected) {
itemStackHasComponent(value, DataComponentTypes.POTION_CONTENTS, component -> {
itemStackHasDataComponent(value, DataComponentTypes.POTION_CONTENTS, component -> {
RegistryEntry<Potion> potion = component.potion()
.orElseThrow(() -> new GameTestException("Expected item stack to have potion " + expected.getKey().orElseThrow()));
if (expected != potion) {
Expand All @@ -86,6 +88,16 @@ public static void itemStackHasPotion(ItemStack value, RegistryEntry<Potion> exp
});
}

public static void dataComponentHasEnchantment(ItemEnchantmentsComponent enchantments, Enchantment expected) {
for (RegistryEntry<Enchantment> enchantment : enchantments.getEnchantments()) {
if (enchantment.value() == expected) {
return;
}
}

throw new GameTestException("Expected data component to have enchantment " + expected);
}

public static void entityDoesNotHaveStatusEffect(LivingEntity entity, RegistryEntry<StatusEffect> effect) {
if (entity.getStatusEffect(effect) != null) {
throw new GameTestException("Expected entity to not have the " + effect.getKey().orElseThrow() + " status effect");
Expand Down
56 changes: 55 additions & 1 deletion src/gametest/java/net/errorcraft/itematic/gametest/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.component.DataComponentType;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.test.GameTestException;
import net.minecraft.test.PositionedException;
import net.minecraft.test.TestContext;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
Expand All @@ -26,12 +34,28 @@
public class TestUtil {
private TestUtil() {}

public static ItemStack createItemStackWithSlightDamage(ServerWorld world, RegistryKey<Item> item) {
ItemStack stack = world.itematic$createStack(item);
if (!stack.isDamageable()) {
throw new AssertionError("Item " + item.getValue() + " is not damageable");
}

stack.setDamage(1);
return stack;
}

public static ItemStack createItemStackWithEnchantment(ServerWorld world, RegistryKey<Item> item, Enchantment enchantment) {
ItemStack stack = world.itematic$createStack(item);
stack.addEnchantment(enchantment, 1);
return stack;
}

public static <T extends ItemComponent<T>> T getItemComponent(ItemStack stack, ItemComponentType<T> type) {
return stack.itematic$getComponent(type)
.orElseThrow(() -> new GameTestException("Item " + stack.itematic$key() + " does not contain the " + ItematicRegistries.ITEM_COMPONENT_TYPE.getKey(type).orElseThrow() + " item component"));
}

public static <T> T getComponent(ItemStack stack, DataComponentType<T> type) {
public static <T> T getDataComponent(ItemStack stack, DataComponentType<T> type) {
T component = stack.get(type);
if (component == null) {
throw new GameTestException("Item stack does not contain the " + type + " component");
Expand Down Expand Up @@ -84,4 +108,34 @@ public static void useBlock(TestContext context, BlockPos pos, PlayerEntity play
BlockPos absolutePos = context.getAbsolutePos(pos);
context.useBlock(pos, player, new BlockHitResult(Vec3d.ofCenter(absolutePos), direction, absolutePos, true));
}

@SuppressWarnings("unchecked")
public static <T extends ScreenHandler> T getMenuFromBlock(TestContext context, BlockPos pos, PlayerEntity player, ScreenHandlerType<T> type) {
BlockPos absolutePos = context.getAbsolutePos(pos);
NamedScreenHandlerFactory factory = context.getBlockState(pos).createScreenHandlerFactory(context.getWorld(), absolutePos);
if (factory == null) {
throw new PositionedException("Block does not provide a menu", absolutePos, pos, context.getTick());
}

ScreenHandler menu = factory.createMenu(-1, player.getInventory(), player);
if (menu == null) {
throw new PositionedException("Block does not create a menu", absolutePos, pos, context.getTick());
}

try {
ScreenHandlerType<?> actualType = menu.getType();
if (type == actualType) {
return (T) menu;
}

throw new PositionedException(
"Block has the incorrect menu type " + Registries.SCREEN_HANDLER.getId(actualType) + ", expected " + Registries.SCREEN_HANDLER.getId(type),
absolutePos,
pos,
context.getTick()
);
} catch (UnsupportedOperationException ignored) {
throw new PositionedException("Block does not create a menu by type", absolutePos, pos, context.getTick());
}
}
}
Loading