Skip to content

Commit

Permalink
Update to 1.20.x
Browse files Browse the repository at this point in the history
- Use of Guava Cache for reduced memory
- Removed dependency on fabric API
  • Loading branch information
mattymatty97 committed Jun 25, 2023
1 parent 5a80baf commit 56f1337
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 52 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
modImplementation "carpet:fabric-carpet:${project.minecraft_version}-${project.carpet_core_version}"
implementation 'de.cronn:reflection-util:2.13.2'
provided group: 'org.apache.commons', name: 'commons-collections4', version: 'latest.release'
}


Expand Down
10 changes: 4 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.19.4
yarn_mappings=1.19.4+build.2
minecraft_version=1.20
yarn_mappings=1.20+build.1
loader_version=0.14.21
carpet_core_version=1.4.101+v230319
# Mod Properties
mod_version=1.10.0
mod_suffix=-MC1.19.4
mod_suffix=-MC1.20+
maven_group=carpet-shadow-item
archives_base_name=carpet-shadow
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.84.0+1.19.4
carpet_core_version=1.4.112+v230608
22 changes: 15 additions & 7 deletions src/main/java/com/carpet_shadow/CarpetShadow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
import carpet.CarpetExtension;
import carpet.CarpetServer;
import com.carpet_shadow.utility.RandomString;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CarpetShadow implements CarpetExtension, ModInitializer {
public static final HashMap<String, WeakReference<ItemStack>> shadowMap = new HashMap<>();
public static final Cache<String, ItemStack> shadowMap = CacheBuilder.newBuilder().weakValues().build();
public static final Logger LOGGER = LogManager.getLogger("carpet-shadow");
public static RandomString shadow_id_generator = new RandomString(CarpetShadowSettings.shadowItemIdSize);

Expand All @@ -37,9 +37,17 @@ public void onGameStarted() {
public void onInitialize() {
CarpetServer.manageExtension(new CarpetShadow());
CarpetShadow.LOGGER.info("Carpet Shadow Loading!");
ServerLifecycleEvents.SERVER_STOPPED.register((server -> {
shadowMap.clear();
}));
}

@Override
public void onServerClosed(MinecraftServer server) {
shadowMap.invalidateAll();
}

@Override
public void onTick(MinecraftServer server) {
if (server.getTicks() % 1000 == 0)
CarpetShadow.shadowMap.cleanUp();
}

@Override
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/carpet_shadow/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ public class Globals {
public static ItemStack getByIdOrNull(String shadow_id) {
if(shadow_id == null)
return null;
Reference<ItemStack> reference = CarpetShadow.shadowMap.get(shadow_id);
if (reference != null && !reference.refersTo(null)) {
return reference.get();
}
return null;
return CarpetShadow.shadowMap.getIfPresent(shadow_id);
}

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));
ItemStack reference = CarpetShadow.shadowMap.getIfPresent(shadow_id);
if (reference != null)
return reference;
((ShadowItem)(Object)stack).setShadowId(shadow_id);
CarpetShadow.shadowMap.put(shadow_id, stack);
return stack;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.RecipeInputInventory;
import net.minecraft.item.*;
import net.minecraft.recipe.*;
import net.minecraft.recipe.book.CraftingRecipeCategory;
Expand All @@ -32,7 +33,7 @@ private void addShadowRecipe(Map<Identifier, JsonElement> map, ResourceManager r
Identifier identifier = new Identifier("carpet_shadow","shadow_recipe");
Recipe<?> recipe = new ShapelessRecipe(identifier, "shadow", CraftingRecipeCategory.MISC, ItemStack.EMPTY, DefaultedList.of()) {
@Override
public boolean matches(CraftingInventory inventory, World world) {
public boolean matches(RecipeInputInventory inventory, World world) {
if (CarpetShadowSettings.shadowItemMode== CarpetShadowSettings.Mode.UNLINK || !CarpetShadowSettings.shadowCraftingGeneration)
return false;

Expand All @@ -50,7 +51,7 @@ public boolean matches(CraftingInventory inventory, World world) {
}

@Override
public ItemStack craft(CraftingInventory inventory, DynamicRegistryManager registryManager) {
public ItemStack craft(RecipeInputInventory inventory, DynamicRegistryManager registryManager) {
if (CarpetShadowSettings.shadowItemMode== CarpetShadowSettings.Mode.UNLINK || !CarpetShadowSettings.shadowCraftingGeneration)
return ItemStack.EMPTY;

Expand Down Expand Up @@ -85,6 +86,10 @@ public boolean fits(int width, int height) {
return width * height >= 2;
}

@Override
public boolean isIgnoredInRecipeBook() {
return true;
}
};
((ImmutableMap.Builder)map2.computeIfAbsent(recipe.getType(), recipeType -> ImmutableMap.builder())).put(identifier, recipe);
builder.put(identifier, recipe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
@Mixin(ItemEntity.class)
public abstract class ItemEntityMixin {

@Redirect(method = "merge(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;I)Lnet/minecraft/item/ItemStack;", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;copy()Lnet/minecraft/item/ItemStack;"))
private static ItemStack redirect_copy(ItemStack stack) {
@Redirect(method = "merge(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;I)Lnet/minecraft/item/ItemStack;", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;copyWithCount(I)Lnet/minecraft/item/ItemStack;"))
private static ItemStack redirect_copy(ItemStack stack, int count) {
if (CarpetShadowSettings.shadowItemInventoryFragilityFix && ((ShadowItem) (Object) stack).getShadowId() != null) {
return stack;
}
return stack.copy();
return stack.copyWithCount(count);
}

@Inject(method = "canMerge(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;)Z", at = @At("RETURN"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public void setEntity(ItemEntity entity) {
this.entity = entity;
}

@Inject(method = "isItemEqual", at = @At("RETURN"), cancellable = true)
private void check_EqualIgnoreDamage(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
Globals.shadow_merge_check(stack, (ItemStack) (Object) this, cir);
@Inject(method = "areItemsEqual", at = @At("RETURN"), cancellable = true)
private static void check_EqualIgnoreDamage(ItemStack left, ItemStack right, CallbackInfoReturnable<Boolean> cir) {
Globals.shadow_merge_check(left, right, cir);
}

@Inject(method = "isEqual", at = @At("RETURN"), cancellable = true)
private void check_Equal(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
@Inject(method = "areEqual", at = @At("RETURN"), cancellable = true)
private static void check_Equal(ItemStack left, ItemStack right, CallbackInfoReturnable<Boolean> cir) {
if (CarpetShadowSettings.shadowItemInventoryFragilityFix && cir.getReturnValue()) {
String shadow1 = ((ShadowItem) (Object) stack).getShadowId();
String shadow2 = ((ShadowItem) (Object) this).getShadowId();
String shadow1 = ((ShadowItem) (Object) left).getShadowId();
String shadow2 = ((ShadowItem) (Object) right).getShadowId();
if (!Objects.equals(shadow1, shadow2)) {
cir.setReturnValue(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.context.LootContext;
import net.minecraft.server.world.ServerWorld;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -14,7 +15,7 @@
@Mixin(LootTable.class)
public class LootTableMixin {
@Inject(method = "processStacks", at=@At("RETURN"), cancellable = true)
private static void fix_survival_shulkers(LootContext lootContext, Consumer<ItemStack> lootConsumer, CallbackInfoReturnable<Consumer<ItemStack>> cir) {
private static void fix_survival_shulkers(ServerWorld world, Consumer<ItemStack> lootConsumer, CallbackInfoReturnable<Consumer<ItemStack>> cir) {
Consumer<ItemStack> consumer = cir.getReturnValue();
Consumer<ItemStack> ret = itemStack -> {
if (CarpetShadowSettings.shadowItemMode != CarpetShadowSettings.Mode.UNLINK &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {

@Inject(at = @At("HEAD"), method = "fromNbt", cancellable = true)
private static void pre_fromNbt(NbtCompound nbt, CallbackInfoReturnable<ItemStack> cir) {
if (CarpetShadowSettings.shadowItemMode.shouldLoadItem() && !CarpetShadowSettings.shadowItemMode.shouldResetCount() && nbt.contains("shadow")) {
Expand All @@ -40,8 +38,8 @@ private static void post_fromNbt(NbtCompound nbt, CallbackInfoReturnable<ItemSta
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);
CarpetShadow.shadowMap.put(shadow_id, stack);
CarpetShadow.LOGGER.debug("Shadowed item loaded from memory");
CarpetShadow.LOGGER.debug("id: " + shadow_id);
}
Expand All @@ -56,10 +54,11 @@ private void post_writeNbt(NbtCompound nbt, CallbackInfoReturnable<NbtCompound>
ItemStack stack = ((ItemStack) (Object) this);
String shadow_id = ((ShadowItem) (Object) stack).getShadowId();
if (shadow_id != null) {
Reference<ItemStack> reference = CarpetShadow.shadowMap.get(shadow_id);
if (reference != null && reference.refersTo(stack)) {
ItemStack reference = CarpetShadow.shadowMap.getIfPresent(shadow_id);
if (reference == stack) {
if (stack.isEmpty()) {
CarpetShadow.shadowMap.remove(shadow_id);
CarpetShadow.shadowMap.invalidate(shadow_id);
((ShadowItem) (Object) stack).setShadowId(null);
} else {
ret.putString("shadow", shadow_id);
cir.setReturnValue(ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public abstract class ItemStackMixin {
@Inject(method = "getTooltip", at = @At("RETURN"))
private void postToolTip(@Nullable PlayerEntity player, TooltipContext context, CallbackInfoReturnable<List<Text>> cir) {
List<Text> list = cir.getReturnValue();
if (CarpetShadowSettings.shadowItemTooltip && ((ShadowItem) (Object) this).getShadowId() != null) {
if (CarpetShadowSettings.shadowItemTooltip && ((ShadowItem) this).getShadowId() != null) {
MutableText text = MutableText.of(new LiteralTextContent("shadow_id: "));
MutableText sub = MutableText.of(new LiteralTextContent(((ShadowItem) (Object) this).getShadowId()));
MutableText sub = MutableText.of(new LiteralTextContent(((ShadowItem) this).getShadowId()));
sub.formatted(Formatting.GOLD, Formatting.BOLD);
text.append(sub);
text.formatted(Formatting.ITALIC);
Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
"carpet-shadow.mixins.json"
],
"depends": {
"fabricloader": ">=0.14.7",
"fabric": ">=0.55.3",
"carpet": ">=1.4.79",
"minecraft": "1.19.x"
"carpet": ">=1.4.112",
"minecraft": "1.20.x"
},
"custom": {
"modmenu": {
Expand Down

0 comments on commit 56f1337

Please sign in to comment.