Skip to content

Commit

Permalink
Add toggleable basic features
Browse files Browse the repository at this point in the history
  • Loading branch information
kikugie committed Oct 13, 2024
1 parent 233d97f commit eef64cd
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 22 deletions.
32 changes: 32 additions & 0 deletions src/main/java/dev/kikugie/shulkerfix/ShulkerFixProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.kikugie.shulkerfix;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.WorldSavePath;

import java.nio.file.Files;
import java.nio.file.Path;

public class ShulkerFixProperties {
public boolean seenRuleNotification;

public ShulkerFixProperties(boolean seenRuleNotification) {
this.seenRuleNotification = seenRuleNotification;
}

public void save(MinecraftServer server) {
if (seenRuleNotification) try {
Path marker = server.getSavePath(WorldSavePath.ROOT).resolve("data/shulkerfix.marker");
Files.createDirectories(marker.getParent());
Files.createFile(marker);
Files.writeString(marker, "\uD83C\uDF4C");
} catch (Exception e) {
ShulkerFixMod.LOGGER.warn("Failed to create marker file for shulkerfix", e);
}
}

public static ShulkerFixProperties load(MinecraftServer server) {
Path marker = server.getSavePath(WorldSavePath.ROOT).resolve("data/shulkerfix.marker");
boolean marked = Files.exists(marker);
return new ShulkerFixProperties(marked);
}
}
9 changes: 9 additions & 0 deletions src/main/java/dev/kikugie/shulkerfix/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.kikugie.shulkerfix;

import dev.kikugie.shulkerfix.carpet.ShulkerFixSettings;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.component.DataComponentTypes;
Expand All @@ -10,6 +11,14 @@
import net.minecraft.item.ItemStack;

public class Util {
public static boolean isShulkerBoxChecked(ItemStack stack) {
return !ShulkerFixSettings.hopperShulkerStacking && isShulkerBox(stack);
}

public static boolean isShulkerBoxLimited(ItemStack stack) {
return ShulkerFixSettings.overstackedShulkerSignalStrength && isShulkerBox(stack);
}

public static boolean isShulkerBox(ItemStack stack) {
return stack.getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof ShulkerBoxBlock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import carpet.CarpetExtension;
import carpet.CarpetServer;
import carpet.api.settings.SettingsManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import dev.kikugie.shulkerfix.ShulkerFixMod;
import dev.kikugie.shulkerfix.ShulkerFixProperties;
import dev.kikugie.shulkerfix.mixin.compat.CarpetSettingsManagerAccessor;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,12 +36,36 @@ public void onGameStarted() {
CarpetServer.settingsManager.parseSettingsClass(ShulkerFixSettings.class);
}

@Override
public void onServerLoadedWorlds(MinecraftServer server) {
ShulkerFixProperties properties = ShulkerFixProperties.load(server);
if (!properties.seenRuleNotification) try {
ShulkerFixSettings.hopperShulkerStacking = false;
ShulkerFixSettings.overstackedShulkerSignalStrength = true;

SettingsManager manager = CarpetServer.settingsManager;
((CarpetSettingsManagerAccessor) manager).invokeSetDefault(
server.getCommandSource(),
manager.getCarpetRule("hopperShulkerStacking"),
"false");
((CarpetSettingsManagerAccessor) manager).invokeSetDefault(
server.getCommandSource(),
manager.getCarpetRule("overstackedShulkerSignalStrength"),
"true");
properties.seenRuleNotification = true;
} catch (Exception e) {
ShulkerFixMod.LOGGER.warn("Failed to save rule defaults", e);
}
properties.save(server);
}

@Override
public Map<String, String> canHasTranslations(String lang) {
try (InputStream stream = ShulkerFixSettings.class.getClassLoader().getResourceAsStream("assets/shulkerfix/lang/%s.json".formatted(lang))) {
if (stream == null) return Collections.emptyMap();
Gson gson = new Gson();
TypeToken<Map<String, String>> token = new TypeToken<>() {};
TypeToken<Map<String, String>> token = new TypeToken<>() {
};
Reader reader = new InputStreamReader(stream);
return gson.fromJson(reader, token.getType());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class ShulkerFixSettings {
public static final String SHULKERFIX = "shulkerfix";

@Rule(categories = {SHULKERFIX, RuleCategory.BUGFIX})
public static boolean hopperShulkerStacking = true;

@Rule(categories = {SHULKERFIX, RuleCategory.BUGFIX})
public static boolean overstackedShulkerSignalStrength = false;

@Rule(categories = {SHULKERFIX, RuleCategory.BUGFIX, RuleCategory.EXPERIMENTAL})
public static boolean clientShulkerSync = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ public class HopperBlockEntityMixin {
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I")
)
private int modifyShulkerMaxCount(ItemStack instance, Operation<Integer> original) {
return Util.isShulkerBox(instance) ? instance.getCount() : original.call(instance);
return Util.isShulkerBoxChecked(instance) ? instance.getCount() : original.call(instance);
}

@WrapOperation(
method = "isInventoryFull",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I")
)
private static int modifyShulkerMaxCountStatic(ItemStack instance, Operation<Integer> original) {
return Util.isShulkerBox(instance) ? 1 : original.call(instance);
return Util.isShulkerBoxChecked(instance) ? 1 : original.call(instance);
}

@WrapOperation(
method = "extract(Lnet/minecraft/world/World;Lnet/minecraft/block/entity/Hopper;)Z",
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z")
)
private static boolean limitCollectCount(Inventory inventory, ItemEntity itemEntity, Operation<Boolean> original) {
if (!Util.isShulkerBox(itemEntity.getStack())) return original.call(inventory, itemEntity);
if (!Util.isShulkerBoxChecked(itemEntity.getStack())) return original.call(inventory, itemEntity);
if (ShulkerFixSettings.hopperCollectSingleShulkers && inventory instanceof HopperBlockEntity)
return Util.collectOneItem(inventory, itemEntity);
else if (ShulkerFixSettings.minecartCollectSingleShulkers && inventory instanceof HopperMinecartEntity)
Expand All @@ -52,7 +52,7 @@ else if (ShulkerFixSettings.minecartCollectSingleShulkers && inventory instanceo
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z")
)
private static boolean limitHopperCollectCount(Inventory inventory, ItemEntity itemEntity, Operation<Boolean> original) {
if (!Util.isShulkerBox(itemEntity.getStack())) return original.call(inventory, itemEntity);
if (!Util.isShulkerBoxChecked(itemEntity.getStack())) return original.call(inventory, itemEntity);
if (ShulkerFixSettings.hopperCollectSingleShulkers)
return Util.collectOneItem(inventory, itemEntity);
else return original.call(inventory, itemEntity);
Expand All @@ -64,6 +64,6 @@ private static boolean limitHopperCollectCount(Inventory inventory, ItemEntity i
cancellable = true
)
private static void cancelItemMerging(ItemStack first, ItemStack second, CallbackInfoReturnable<Boolean> cir) {
if (Util.isShulkerBox(first) || Util.isShulkerBox(second)) cir.setReturnValue(false);
if (Util.isShulkerBoxChecked(first) || Util.isShulkerBox(second)) cir.setReturnValue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

@Mixin(HopperMinecartEntity.class)
public class HopperMinecartEntityMixin {
@WrapOperation(
method = "canOperate",
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z")
)
private boolean limitCollectItems(Inventory inventory, ItemEntity itemEntity, Operation<Boolean> original) {
return Util.isShulkerBox(itemEntity.getStack()) && ShulkerFixSettings.minecartCollectSingleShulkers ? Util.collectOneItem(inventory, itemEntity) : original.call(inventory, itemEntity);
}
@WrapOperation(
method = "canOperate",
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z")
)
private boolean limitCollectItems(Inventory inventory, ItemEntity itemEntity, Operation<Boolean> original) {
return Util.isShulkerBoxChecked(itemEntity.getStack())
&& ShulkerFixSettings.minecartCollectSingleShulkers
? Util.collectOneItem(inventory, itemEntity)
: original.call(inventory, itemEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ScreenHandlerMixin {
at = @At(value = "INVOKE", target = "Lnet/minecraft/inventory/Inventory;getMaxCount(Lnet/minecraft/item/ItemStack;)I")
)
private static int fixOverstackedSignalStrength(Inventory instance, ItemStack stack, Operation<Integer> original) {
return !Util.isShulkerBox(stack) || Util.isWrapped(instance) ? original.call(instance, stack) : 1;
return !Util.isShulkerBoxLimited(stack) || Util.isWrapped(instance) ? original.call(instance, stack) : 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.kikugie.shulkerfix.mixin.compat;

import carpet.api.settings.CarpetRule;
import carpet.api.settings.SettingsManager;
import net.minecraft.server.command.ServerCommandSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(value = SettingsManager.class, remap = false)
public interface CarpetSettingsManagerAccessor {
@Invoker
@SuppressWarnings("UnusedReturnValue")
int invokeSetDefault(ServerCommandSource source, CarpetRule<?> rule, String stringValue);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.kikugie.shulkerfix.mixin;
package dev.kikugie.shulkerfix.mixin.compat;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
Expand All @@ -23,6 +23,6 @@ public class LithiumHopperHelperMixin {
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I")
)
private static int modifyShulkerMaxCount(ItemStack instance, Operation<Integer> original) {
return Util.isShulkerBox(instance) ? 1 : original.call(instance);
return Util.isShulkerBoxChecked(instance) ? 1 : original.call(instance);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.kikugie.shulkerfix.mixin;
package dev.kikugie.shulkerfix.mixin.compat;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
Expand All @@ -17,6 +17,6 @@ public class LithiumStackListMixin {
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getMaxCount()I")
)
private int get1(ItemStack instance, Operation<Integer> original) {
return Util.isShulkerBox(instance) ? 1 : original.call(instance);
return Util.isShulkerBoxLimited(instance) ? 1 : original.call(instance);
}
}
8 changes: 7 additions & 1 deletion src/main/resources/assets/shulkerfix/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"carpet.category.shulkerfix": "ShulkerFix",
"carpet.rule.hopperShulkerStacking.name": "Hopper Shulker Box Stacking",
"carpet.rule.hopperShulkerStacking.desc": "Controls whenever hopper transfers should stack shulkers in containers.",
"carpet.rule.overstackedShulkerSignalStrength.name": "Overstacked Shulker Signal Strength",
"carpet.rule.overstackedShulkerSignalStrength.desc": "Makes stacked shulkers create stronger comparator output, also known as OSS/HSS.",
"carpet.rule.clientShulkerSync.name": "Sync Stacked Shulker Count",
"carpet.rule.clientShulkerSync.desc": "Fixes displayed shulker amount on vanilla clients. See issue #1899 in Carpet's repo.",
"carpet.rule.hopperCollectSingleShulkers.name": "Hoppers Collect Single Shulkers",
"carpet.rule.hopperCollectSingleShulkers.desc": "Makes hoppers pick up single shulkers from a stack entity.",
"carpet.rule.minecartCollectSingleShulkers.name": "Hopper Minecarts Collect Single Shulkers",
"carpet.rule.minecartCollectSingleShulkers.desc": "Makes hopper minecarts pick up single shulkers from a stack entity.",
"carpet.rule.overstackedMinecartSlowdown.name": "Overstacked Minecart Slowdown",
"carpet.rule.overstackedMinecartSlowdown.desc": "Disables additional slowdown caused by overstacked shulker boxes when set to false."
"carpet.rule.overstackedMinecartSlowdown.desc": "Disables additional slowdown caused by overstacked shulker boxes when set to false.",

"shulkerfix.message.seenRuleNotification": "§a[ShulkerFix]§r The default functionality of this mod has been moved to §bhopperShulkerStacking§b and §boverstackedShulkerSignalStrength§r Carpet rules. For compatibility reasons the functionality has been enabled for this world. This only happens for the first time a world is loaded and you can disable the rules if you want."
}
6 changes: 3 additions & 3 deletions src/main/resources/shulkerfix-compat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"package": "dev.kikugie.shulkerfix.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"HopperMinecartEntityMixin",
"ItemStackCodecMixin",
"LithiumHopperHelperMixin",
"LithiumStackListMixin"
"compat.CarpetSettingsManagerAccessor",
"compat.LithiumHopperHelperMixin",
"compat.LithiumStackListMixin"
]
}
1 change: 1 addition & 0 deletions src/main/resources/shulkerfix.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "dev.kikugie.shulkerfix.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"HopperMinecartEntityMixin",
"HopperBlockEntityMixin",
"ScreenHandlerMixin",
"StorageMinecartEntityMixin"
Expand Down

0 comments on commit eef64cd

Please sign in to comment.