Skip to content

Commit

Permalink
merge: #6
Browse files Browse the repository at this point in the history
Mixin refactoring and TextContents in translation files
  • Loading branch information
CallMeEchoCodes authored Oct 6, 2024
2 parents a288d66 + e21e546 commit 674f82f
Show file tree
Hide file tree
Showing 33 changed files with 615 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.api.block.BlockMetatags;
import dev.spiritstudios.specter.impl.block.SpecterBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.AxeItem;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Map;
import java.util.Optional;
Expand All @@ -25,19 +20,25 @@
public class AxeItemMixin {
@Shadow
@Final
public static Map<Block, Block> STRIPPED_BLOCKS;
protected static Map<Block, Block> STRIPPED_BLOCKS;

@WrapOperation(method = "tryStrip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/AxeItem;getStrippedState(Lnet/minecraft/block/BlockState;)Ljava/util/Optional;"))
private Optional<BlockState> getStrippedState(AxeItem instance, BlockState state, Operation<Optional<BlockState>> original) {
Optional<Block> strippedBlock = BlockMetatags.STRIPPABLE.get(state.getBlock());
if (strippedBlock.isEmpty()) strippedBlock = Optional.ofNullable(STRIPPED_BLOCKS.get(state.getBlock()));

return strippedBlock.map(block -> block.getStateWithProperties(state));
return Optional.ofNullable(STRIPPED_BLOCKS.get(state.getBlock()))
.or(() -> BlockMetatags.STRIPPABLE.get(state.getBlock()))
.map(block -> block.getStateWithProperties(state))
.or(() -> original.call(instance, state));
}

@Inject(method = "tryStrip", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ofNullable(Ljava/lang/Object;)Ljava/util/Optional;"), cancellable = true)
private void tryStrip(World world, BlockPos pos, @Nullable PlayerEntity player, BlockState state, CallbackInfoReturnable<Optional<BlockState>> cir) {
Optional<BlockState> unwaxedBlockState = Optional.ofNullable(SpecterBlock.WAXED_TO_UNWAXED_BLOCKS.get(state.getBlock())).map(unwaxed -> unwaxed.getStateWithProperties(state));
if (unwaxedBlockState.isPresent()) cir.setReturnValue(unwaxedBlockState);
@WrapOperation(method = "tryStrip", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ofNullable(Ljava/lang/Object;)Ljava/util/Optional;"))
private Optional<Object> tryStrip(
Object value,
Operation<Optional<Object>> original,
@Local(argsOnly = true) BlockState state
) {
Optional<Object> unwaxedBlock = Optional.ofNullable(SpecterBlock.WAXED_TO_UNWAXED_BLOCKS.get(state.getBlock()))
.map(block -> block.getStateWithProperties(state));

return unwaxedBlock.or(() -> original.call(value));
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
package dev.spiritstudios.specter.mixin.block;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.api.block.BlockMetatags;
import dev.spiritstudios.specter.api.block.FlammableBlockData;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FireBlock;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

@Mixin(FireBlock.class)
public class FireBlockMixin {
@Inject(method = "getBurnChance(Lnet/minecraft/block/BlockState;)I", at = @At("HEAD"), cancellable = true)
private void getBurnChanceFromMetatag(BlockState state, CallbackInfoReturnable<Integer> cir) {
Optional<FlammableBlockData> data = BlockMetatags.FLAMMABLE.get(state.getBlock());
data.ifPresent(flammableBlockData -> cir.setReturnValue(flammableBlockData.burn()));
@WrapOperation(method = "getBurnChance(Lnet/minecraft/block/BlockState;)I", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/Object2IntMap;getInt(Ljava/lang/Object;)I", remap = false))
private int getBurnChanceFromMetatag(
Object2IntMap<Block> instance,
Object value,
Operation<Integer> original,
@Local(argsOnly = true) BlockState state
) {
return BlockMetatags.FLAMMABLE.get((state).getBlock())
.map(FlammableBlockData::burn)
.orElse(original.call(instance, value));
}

@Inject(method = "getSpreadChance(Lnet/minecraft/block/BlockState;)I", at = @At("HEAD"), cancellable = true)
private void getSpreadChanceFromMetatag(BlockState state, CallbackInfoReturnable<Integer> cir) {
Optional<FlammableBlockData> data = BlockMetatags.FLAMMABLE.get(state.getBlock());
data.ifPresent(flammableBlockData -> cir.setReturnValue(flammableBlockData.spread()));
@WrapOperation(method = "getSpreadChance(Lnet/minecraft/block/BlockState;)I", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/Object2IntMap;getInt(Ljava/lang/Object;)I", remap = false))
private int getSpreadChanceFromMetatag(
Object2IntMap<Block> instance,
Object value,
Operation<Integer> original,
@Local(argsOnly = true) BlockState state
) {
return BlockMetatags.FLAMMABLE.get((state).getBlock())
.map(FlammableBlockData::spread)
.orElse(original.call(instance, value));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package dev.spiritstudios.specter.mixin.block;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.impl.block.SpecterBlock;
import net.minecraft.block.BlockState;
import net.minecraft.item.HoneycombItem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

@Mixin(HoneycombItem.class)
public class HoneycombItemMixin {
@Inject(method = "getWaxedState", at = @At("HEAD"), cancellable = true)
private static void getWaxedState(BlockState state, CallbackInfoReturnable<Optional<BlockState>> cir) {
Optional<BlockState> waxedBlockState = Optional.ofNullable(SpecterBlock.UNWAXED_TO_WAXED_BLOCKS.get(state.getBlock())).map(block -> block.getStateWithProperties(state));
if (waxedBlockState.isPresent()) cir.setReturnValue(waxedBlockState);
@WrapOperation(method = "getWaxedState", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ofNullable(Ljava/lang/Object;)Ljava/util/Optional;"))
private static Optional<Object> getWaxedState(
Object value,
Operation<Optional<Object>> original,
@Local(argsOnly = true) BlockState state
) {
Optional<Object> waxedBlockState = Optional.ofNullable(SpecterBlock.UNWAXED_TO_WAXED_BLOCKS.get(state.getBlock()));

return waxedBlockState.or(() -> original.call(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,41 @@
import com.google.common.collect.BiMap;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.spiritstudios.specter.impl.block.SpecterBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Oxidizable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;

@Mixin(Oxidizable.class)
public interface OxidizableMixin {
@Inject(method = "getIncreasedOxidationBlock", at = @At("HEAD"), cancellable = true)
private static void getIncreasedOxidationBlock(Block block, CallbackInfoReturnable<Optional<Block>> cir) {
Optional<Block> increasedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_INCREASES.get(block));
if (increasedOxidationBlock.isPresent()) cir.setReturnValue(increasedOxidationBlock);
@WrapOperation(method = "getIncreasedOxidationBlock", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ofNullable(Ljava/lang/Object;)Ljava/util/Optional;"))
private static Optional<Object> getIncreasedOxidationBlock(
Object value,
Operation<Optional<Object>> original,
@Local(argsOnly = true) Block block
) {
Optional<Object> increasedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_INCREASES.get(block));
return increasedOxidationBlock.or(() -> original.call(value));
}

@Inject(method = "getDecreasedOxidationBlock", at = @At("HEAD"), cancellable = true)
private static void getDecreasedOxidationBlock(Block block, CallbackInfoReturnable<Optional<Block>> cir) {
Optional<Block> decreasedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_DECREASES.get(block));
if (decreasedOxidationBlock.isPresent()) cir.setReturnValue(decreasedOxidationBlock);
@WrapOperation(method = "getDecreasedOxidationBlock", at = @At(value = "INVOKE", target = "Ljava/util/Optional;ofNullable(Ljava/lang/Object;)Ljava/util/Optional;"))
private static Optional<Object> getDecreasedOxidationBlock(
Object value,
Operation<Optional<Object>> original,
@Local(argsOnly = true) Block block
) {
Optional<Object> decreasedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_DECREASES.get(block));
return decreasedOxidationBlock.or(() -> original.call(value));
}

@SuppressWarnings("rawtypes")
@WrapOperation(method = "getUnaffectedOxidationBlock", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/BiMap;get(Ljava/lang/Object;)Ljava/lang/Object;", remap = false))
private static Object getUnaffectedOxidationBlock(BiMap instance, Object o, Operation<Object> original) {
Block block = (Block) o;
Optional<Block> unaffectedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_DECREASES.get(block));
return unaffectedOxidationBlock.orElseGet(() -> (Block) original.call(instance, o));
private static <K, V> Object getUnaffectedOxidationBlock(BiMap<K, V> instance, Object value, Operation<Object> original) {
Optional<Object> unaffectedOxidationBlock = Optional.ofNullable(SpecterBlock.OXIDATION_LEVEL_DECREASES.get((Block) value));
return unaffectedOxidationBlock.orElseGet(() -> original.call(instance, value));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.spiritstudios.specter.api.block.BlockMetatags;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.ShovelItem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Map;
import java.util.Optional;

@Mixin(ShovelItem.class)
public class ShovelItemMixin {
Expand All @@ -19,7 +17,8 @@ public class ShovelItemMixin {
private <K, V> V get(Map<K, V> instance, Object o, Operation<V> original) {
if (!(o instanceof Block block)) return original.call(instance, o);

Optional<BlockState> flattenedBlock = BlockMetatags.FLATTENABLE.get(block);
return flattenedBlock.map(blockState -> (V) blockState).orElseGet(() -> original.call(instance, o));
return BlockMetatags.FLATTENABLE.get(block)
.map(blockState -> (V) blockState)
.orElseGet(() -> original.call(instance, o));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,4 @@ public void testOxidizableMetatag(TestContext context) {

context.complete();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void testTomlConfig(TestContext context) throws IOException {
context.assertTrue(TestConfig.TOML_HOLDER.load(), "Config file failed to load");
context.assertTrue(Files.exists(path), "Config file does not exist");
context.assertTrue(TestConfig.TOML_HOLDER.get().testString.get().equals("test2"), "String is not equal to test2, Make sure you haven't modified the config");
Files.deleteIfExists(path);

context.complete();
}
Expand All @@ -59,6 +60,7 @@ public void testJsonCConfig(TestContext context) throws IOException {
context.assertTrue(TestConfig.JSON_HOLDER.load(), "Config file failed to load");
context.assertTrue(Files.exists(path), "Config file does not exist");
context.assertTrue(TestConfig.JSON_HOLDER.get().testString.get().equals("test2"), "String is not equal to test2, Make sure you haven't modified the config");
Files.deleteIfExists(path);

context.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RunArgsNetworkMixin {
@Mutable
public Session session;

@Inject(method = "<init>", at = @At("TAIL"))
@Inject(method = "<init>", at = @At("RETURN"))
private void init(Session session, PropertyMap userProperties, PropertyMap profileProperties, Proxy proxy, CallbackInfo ci) {
if (!SpecterGlobals.DEBUG) return;

Expand All @@ -35,7 +35,7 @@ private void init(Session session, PropertyMap userProperties, PropertyMap profi
String username = System.getProperty("specter.development.username");
UUID uuid = UndashedUuid.fromString(System.getProperty("specter.development.uuid").replace("-", ""));

SpecterGlobals.LOGGER.info(String.format("Using development account %s (%s)", username, uuid));
SpecterGlobals.LOGGER.info("Using development account {} ({})", username, uuid);
this.session = new Session(
username,
uuid,
Expand Down
10 changes: 9 additions & 1 deletion specter-debug/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
moduleDependencies(project, "specter-core", "specter-registry", "specter-item", "specter-block")
moduleDependencies(
project,
"specter-core",
"specter-registry",
"specter-item",
"specter-block",
"specter-serialization",
"specter-render"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@

import dev.spiritstudios.specter.api.block.BlockMetatags;
import dev.spiritstudios.specter.api.item.ItemMetatags;
import dev.spiritstudios.specter.api.render.RenderMetatags;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.minecraft.block.Block;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.List;

public class SpecterDebugClient implements ClientModInitializer {
private static Text getMetatagHeader(String key) {
return Text.translatable("tooltip.metatag." + key).formatted(Formatting.DARK_PURPLE);
}

private static Text getMetatagText(String key, Object... values) {
return Text.translatable("tooltip.metatag." + key + ".text", values).formatted(Formatting.BLUE);
}

private static void addMetatagLine(List<Text> lines, String key, Object... values) {
lines.add(getMetatagHeader(key));
lines.add(getMetatagText(key, values));
lines.add(Text.translatable("tooltip.metatag." + key));
lines.add(Text.translatable("tooltip.metatag." + key + ".text", values));
}

@Override
Expand All @@ -40,6 +32,8 @@ public void onInitializeClient() {
BlockMetatags.OXIDIZABLE.get(block).ifPresent(entry -> addMetatagLine(lines, "oxidizable", entry.getName()));
BlockMetatags.STRIPPABLE.get(block).ifPresent(entry -> addMetatagLine(lines, "strippable", entry.getName()));
BlockMetatags.WAXABLE.get(block).ifPresent(entry -> addMetatagLine(lines, "waxable", entry.getName()));

RenderMetatags.RENDER_LAYER.get(block).ifPresent(entry -> addMetatagLine(lines, "render_layer", entry.asString()));
});
}
}
Loading

0 comments on commit 674f82f

Please sign in to comment.