From 0ddd63e1fbd7ba2d5a4460b899b9e49f89a80fc9 Mon Sep 17 00:00:00 2001 From: The Matty Date: Fri, 12 Nov 2021 11:32:49 +0100 Subject: [PATCH] V 1.6.1 - BETA-1.6.5 Working BackPort --- build.properties | 4 +-- .../java/com/carpet_shadow/CarpetShadow.java | 12 +++++++- .../mixins/general/ScreenHandlerMixin.java | 30 +++++++++++++++---- src/main/resources/fabric.mod.json | 6 ++-- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/build.properties b/build.properties index 3034467..55d19d9 100644 --- a/build.properties +++ b/build.properties @@ -1,3 +1,3 @@ -#Thu Nov 11 21:11:45 CET 2021 +#Fri Nov 12 11:24:53 CET 2021 mod_version=1.6.1 -mod_buildnumber=4 +mod_buildnumber=5 diff --git a/src/main/java/com/carpet_shadow/CarpetShadow.java b/src/main/java/com/carpet_shadow/CarpetShadow.java index 2afe8e1..3f426fb 100644 --- a/src/main/java/com/carpet_shadow/CarpetShadow.java +++ b/src/main/java/com/carpet_shadow/CarpetShadow.java @@ -3,6 +3,7 @@ import carpet.CarpetExtension; import carpet.CarpetServer; import com.carpet_shadow.utility.RandomString; +import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.minecraft.item.ItemStack; @@ -12,7 +13,7 @@ import java.lang.ref.WeakReference; import java.util.HashMap; -public class CarpetShadow implements CarpetExtension, ModInitializer { +public class CarpetShadow implements CarpetExtension, ModInitializer, ClientModInitializer { public static final HashMap> shadowMap = new HashMap<>(); public static final Logger LOGGER = LogManager.getLogger("carpet-shadow"); public static RandomString shadow_id_generator = new RandomString(CarpetShadowSettings.shadowItemIdSize); @@ -32,4 +33,13 @@ public void onInitialize() { shadowMap.clear(); })); } + + @Override + public void onInitializeClient() { + CarpetServer.manageExtension(new CarpetShadow()); + LOGGER.info("Carpet Shadow Loading!"); + ServerLifecycleEvents.SERVER_STOPPED.register((server -> { + shadowMap.clear(); + })); + } } diff --git a/src/main/java/com/carpet_shadow/mixins/general/ScreenHandlerMixin.java b/src/main/java/com/carpet_shadow/mixins/general/ScreenHandlerMixin.java index e2913e5..9ed81fa 100644 --- a/src/main/java/com/carpet_shadow/mixins/general/ScreenHandlerMixin.java +++ b/src/main/java/com/carpet_shadow/mixins/general/ScreenHandlerMixin.java @@ -21,9 +21,9 @@ public abstract class ScreenHandlerMixin { @Redirect(method = "method_30010", - at = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;setStack(Lnet/minecraft/item/ItemStack;)V", ordinal = 0), + at = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;setStack(Lnet/minecraft/item/ItemStack;)V",ordinal = 2), slice = @Slice( - from = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;getMaxItemCount(Lnet/minecraft/item/ItemStack;)I", ordinal = 3)), + from = @At(value = "FIELD", target = "Lnet/minecraft/screen/slot/SlotActionType;SWAP:Lnet/minecraft/screen/slot/SlotActionType;")), require = 0) private void handle_shadowing(Slot instance, ItemStack stack) { try { @@ -38,11 +38,29 @@ private void handle_shadowing(Slot instance, ItemStack stack) { CarpetShadow.shadowMap.put(shadow_id, new WeakReference<>(stack)); ((ShadowItem) (Object) stack).setShadowId(shadow_id); } - if (CarpetShadowSettings.shadowItemPersistence) { - throw new ShadowingException(); - } else { - throw error; + throw error; + } + } + + @Redirect(method = "method_30010", + at = @At(value = "INVOKE", target = "Lnet/minecraft/screen/slot/Slot;setStack(Lnet/minecraft/item/ItemStack;)V",ordinal = 4), + slice = @Slice( + from = @At(value = "FIELD", target = "Lnet/minecraft/screen/slot/SlotActionType;SWAP:Lnet/minecraft/screen/slot/SlotActionType;")), + require = 0) + private void handle_shadowing2(Slot instance, ItemStack stack) { + try { + instance.setStack(stack); + } catch (Throwable error) { + CarpetShadow.LOGGER.warn("New Shadow Item Created"); + String shadow_id = ((ShadowItem) (Object) stack).getShadowId(); + if (shadow_id == null) { + do { + shadow_id = CarpetShadow.shadow_id_generator.nextString(); + } while (CarpetShadow.shadowMap.containsKey(shadow_id)); + CarpetShadow.shadowMap.put(shadow_id, new WeakReference<>(stack)); + ((ShadowItem) (Object) stack).setShadowId(shadow_id); } + throw error; } } // diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c89dfb2..e0a2d6a 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -14,7 +14,9 @@ "entrypoints": { "main": [ "com.carpet_shadow.CarpetShadow" - ] + ], + "client": [], + "server": [] }, "mixins": [ "carpet-shadow.mixins.json" @@ -22,7 +24,7 @@ "depends": { "fabricloader": ">=0.11.7", "fabric": ">=0.11.0", - "carpet": ">=1.4.52", + "carpet": ">=1.4.44", "minecraft": "1.16.5" }, "suggests": {