Skip to content

Commit

Permalink
V 1.5 - BETA
Browse files Browse the repository at this point in the history
Fixed QUICK_MOVE ( shift clicking ) fragility.
had to use a compromise: now shadow stacks will behave like unstackables on QUICK_MOVE
  • Loading branch information
mattymatty97 committed Nov 9, 2021
1 parent aaa1246 commit 6cc4258
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 9 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions build.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/carpet_shadow/Globals.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> 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<ItemStack> 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<Boolean> cir){
if(CarpetShadowSettings.shadowItemFragilityFixes && cir.getReturnValue()){
String shadow1 = ((ShadowItem)(Object)stack1).getShadowId();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,11 +20,11 @@ public abstract class ItemStackMixin {
private static void pre_fromNbt(NbtCompound nbt, CallbackInfoReturnable<ItemStack> cir){
if(CarpetShadowSettings.shadowItemPersistence && nbt.contains("shadow")) {
String shadow_id = nbt.getString("shadow");
Reference<ItemStack> 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();
}
}
Expand All @@ -33,8 +34,8 @@ private static void pre_fromNbt(NbtCompound nbt, CallbackInfoReturnable<ItemStac
private static void post_fromNbt(NbtCompound nbt, CallbackInfoReturnable<ItemStack> cir){
if(CarpetShadowSettings.shadowItemPersistence && nbt.contains("shadow")) {
String shadow_id = nbt.getString("shadow");
Reference<ItemStack> 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);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/carpet-shadow.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"fragility.ItemEntityMixin",
"fragility.ItemStackMixin",
"fragility.PlayerInventoryMixin",
"fragility.ScreenHandlerMixin",
"fragility.SlotMixin",
"general.ItemStackMixin",
"general.ScreenHandlerMixin",
Expand Down

0 comments on commit 6cc4258

Please sign in to comment.