diff --git a/build.gradle b/build.gradle index 9f26703..417a73c 100644 --- a/build.gradle +++ b/build.gradle @@ -131,7 +131,10 @@ tasks.register("FinalizeBuild", Copy){ } doLast { mkdir "output/libs" + println "Output File:" + println "${project.archives_base_name}-${project.version} build ${buildnumber}.jar" } + outputs.upToDateWhen { false } } tasks.build.finalizedBy updateBuildNumber diff --git a/build.properties b/build.properties index 6b0e57e..631bf01 100644 --- a/build.properties +++ b/build.properties @@ -1,3 +1,3 @@ -#Tue Nov 09 14:01:36 CET 2021 -mod_version=1.4.1 -mod_buildnumber=17 +#Tue Nov 09 21:09:38 CET 2021 +mod_version=1.5 +mod_buildnumber=7 diff --git a/gradle.properties b/gradle.properties index 23c3b9d..8030948 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ yarn_mappings=1.17.1+build.63 loader_version=0.12.4 carpet_core_version=1.4.52+v211104 # Mod Properties -mod_version=1.4.2 +mod_version=1.5 mod_suffix=-BETA maven_group=carpet-shadow-item archives_base_name=carpet-shadow diff --git a/src/main/java/com/carpet_shadow/Globals.java b/src/main/java/com/carpet_shadow/Globals.java index e1d926d..51d07a9 100644 --- a/src/main/java/com/carpet_shadow/Globals.java +++ b/src/main/java/com/carpet_shadow/Globals.java @@ -1,10 +1,33 @@ package com.carpet_shadow; import com.carpet_shadow.interfaces.ShadowItem; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; + public class Globals { + + public static ItemStack getByIdOrNull (String shadow_id){ + Reference reference = CarpetShadow.shadowMap.get(shadow_id); + if (reference != null && !reference.refersTo(null)){ + return reference.get(); + } + return null; + } + + public static ItemStack getByIdOrAdd (String shadow_id, ItemStack stack){ + Reference reference = CarpetShadow.shadowMap.get(shadow_id); + if (reference != null && !reference.refersTo(null)){ + return reference.get(); + } + CarpetShadow.shadowMap.put(shadow_id,new WeakReference<>(stack)); + return stack; + } + + public static void shadow_merge_check(ItemStack stack1, ItemStack stack2, CallbackInfoReturnable cir){ if(CarpetShadowSettings.shadowItemFragilityFixes && cir.getReturnValue()){ String shadow1 = ((ShadowItem)(Object)stack1).getShadowId(); diff --git a/src/main/java/com/carpet_shadow/mixins/fragility/ScreenHandlerMixin.java b/src/main/java/com/carpet_shadow/mixins/fragility/ScreenHandlerMixin.java new file mode 100644 index 0000000..a171168 --- /dev/null +++ b/src/main/java/com/carpet_shadow/mixins/fragility/ScreenHandlerMixin.java @@ -0,0 +1,98 @@ +package com.carpet_shadow.mixins.fragility; + +import com.carpet_shadow.CarpetShadowSettings; +import com.carpet_shadow.Globals; +import com.carpet_shadow.interfaces.ShadowItem; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.slot.Slot; +import net.minecraft.screen.slot.SlotActionType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.Slice; +@Mixin(ScreenHandler.class) +public abstract class ScreenHandlerMixin { + + + @Shadow protected abstract boolean insertItem(ItemStack stack, int startIndex, int endIndex, boolean fromLast); + + @Shadow protected abstract void internalOnSlotClick(int slotIndex, int button, SlotActionType actionType, PlayerEntity player); + + String shadowId1 = null; + @Redirect(method = "internalOnSlotClick", slice = @Slice( + from=@At(value = "INVOKE",target = "Lnet/minecraft/screen/slot/Slot;canTakeItems(Lnet/minecraft/entity/player/PlayerEntity;)Z",ordinal = 1)), + at=@At(value = "INVOKE",target = "Lnet/minecraft/screen/slot/Slot;setStack(Lnet/minecraft/item/ItemStack;)V",ordinal = 0) + ) + public void remove_shadow_stack(Slot instance, ItemStack stack){ + shadowId1 = ((ShadowItem)(Object)stack).getShadowId(); + instance.setStack(stack); + } + + @Redirect(method = "internalOnSlotClick", slice = @Slice( + from=@At(value = "INVOKE",target = "Lnet/minecraft/screen/slot/Slot;canTakeItems(Lnet/minecraft/entity/player/PlayerEntity;)Z",ordinal = 1)), + at=@At(value = "INVOKE",target = "Lnet/minecraft/screen/ScreenHandler;setCursorStack(Lnet/minecraft/item/ItemStack;)V", ordinal = 1) + ) + public void remove_shadow_stack(ScreenHandler instance, ItemStack stack){ + String shadowId2 = ((ShadowItem)(Object)stack).getShadowId(); + if(CarpetShadowSettings.shadowItemFragilityFixes && shadowId1 != null && shadowId1.equals(shadowId2)){ + instance.setCursorStack(ItemStack.EMPTY); + }else{ + instance.setCursorStack(stack); + } + shadowId1 = null; + } + + @Redirect(method = "internalOnSlotClick", slice=@Slice( + from = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;canTakeItems(Lnet/minecraft/entity/player/PlayerEntity;)Z") + ), + at=@At(value = "INVOKE",target = "Lnet/minecraft/screen/ScreenHandler;transferSlot(Lnet/minecraft/entity/player/PlayerEntity;I)Lnet/minecraft/item/ItemStack;")) + public ItemStack fix_shift(ScreenHandler instance, PlayerEntity player, int index){ + if(CarpetShadowSettings.shadowItemFragilityFixes){ + Slot og = instance.slots.get(index); + ItemStack og_item = og.getStack(); + if(((ShadowItem)(Object)og_item).getShadowId() != null){ + ItemStack mirror = og_item.copy(); + ((ShadowItem)(Object)mirror).setShadowId(((ShadowItem)(Object)og_item).getShadowId()); + og.setStack(mirror); + ItemStack ret = instance.transferSlot(player,index); + if(ret == ItemStack.EMPTY){ + og_item = Globals.getByIdOrAdd(((ShadowItem)(Object)og_item).getShadowId(), og_item); + og.setStack(og_item); + og_item.setCount(mirror.getCount()); + } + return ret; + } + } + return instance.transferSlot(player,index); + } + + @Redirect(method = "insertItem", slice=@Slice( + from = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;getStack()Lnet/minecraft/item/ItemStack;",ordinal = 1) + ), + at=@At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;split(I)Lnet/minecraft/item/ItemStack;",ordinal = 1)) + public ItemStack fix_shift(ItemStack instance, int amount){ + if(CarpetShadowSettings.shadowItemFragilityFixes && ((ShadowItem)(Object)instance).getShadowId() != null){ + String shadow_id = ((ShadowItem)(Object)instance).getShadowId(); + ItemStack og_item = Globals.getByIdOrNull(shadow_id); + if(og_item != null){ + og_item.setCount(instance.getCount()); + instance.setCount(0); + return og_item; + } + } + return instance.split(amount); + } + + @Redirect(method = "insertItem", + at=@At(value = "INVOKE",target = "Lnet/minecraft/item/ItemStack;isStackable()Z",ordinal = 0)) + public boolean fix_shift2(ItemStack instance){ + if(CarpetShadowSettings.shadowItemFragilityFixes && ((ShadowItem)(Object)instance).getShadowId() != null){ + return false; + } + return instance.isStackable(); + } + +} diff --git a/src/main/java/com/carpet_shadow/mixins/persistence/ItemStackMixin.java b/src/main/java/com/carpet_shadow/mixins/persistence/ItemStackMixin.java index d169152..0097a92 100644 --- a/src/main/java/com/carpet_shadow/mixins/persistence/ItemStackMixin.java +++ b/src/main/java/com/carpet_shadow/mixins/persistence/ItemStackMixin.java @@ -2,6 +2,7 @@ import com.carpet_shadow.CarpetShadow; import com.carpet_shadow.CarpetShadowSettings; +import com.carpet_shadow.Globals; import com.carpet_shadow.interfaces.ShadowItem; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; @@ -19,11 +20,11 @@ public abstract class ItemStackMixin { private static void pre_fromNbt(NbtCompound nbt, CallbackInfoReturnable cir){ if(CarpetShadowSettings.shadowItemPersistence && nbt.contains("shadow")) { String shadow_id = nbt.getString("shadow"); - Reference reference = CarpetShadow.shadowMap.get(shadow_id); - if (reference != null && !reference.refersTo(null)) { + ItemStack reference = Globals.getByIdOrNull(shadow_id); + if (reference != null) { CarpetShadow.LOGGER.debug("Shadowed item restored"); CarpetShadow.LOGGER.debug("id: " + shadow_id); - cir.setReturnValue(reference.get()); + cir.setReturnValue(reference); cir.cancel(); } } @@ -33,8 +34,8 @@ private static void pre_fromNbt(NbtCompound nbt, CallbackInfoReturnable cir){ if(CarpetShadowSettings.shadowItemPersistence && nbt.contains("shadow")) { String shadow_id = nbt.getString("shadow"); - Reference reference = CarpetShadow.shadowMap.get(shadow_id); - if (reference == null || reference.refersTo(null)) { + ItemStack reference = Globals.getByIdOrNull(shadow_id); + if (reference == null) { ItemStack stack = cir.getReturnValue(); CarpetShadow.shadowMap.put(shadow_id,new WeakReference<>(stack)); ((ShadowItem) (Object) stack).setShadowId(shadow_id); diff --git a/src/main/resources/carpet-shadow.mixins.json b/src/main/resources/carpet-shadow.mixins.json index 52ed413..300f415 100644 --- a/src/main/resources/carpet-shadow.mixins.json +++ b/src/main/resources/carpet-shadow.mixins.json @@ -9,6 +9,7 @@ "fragility.ItemEntityMixin", "fragility.ItemStackMixin", "fragility.PlayerInventoryMixin", + "fragility.ScreenHandlerMixin", "fragility.SlotMixin", "general.ItemStackMixin", "general.ScreenHandlerMixin",