diff --git a/src/main/java/dev/micle/geologistpicktweaks/config/Config.java b/src/main/java/dev/micle/geologistpicktweaks/config/Config.java index cd0c56e..1840066 100644 --- a/src/main/java/dev/micle/geologistpicktweaks/config/Config.java +++ b/src/main/java/dev/micle/geologistpicktweaks/config/Config.java @@ -61,7 +61,15 @@ public final class Config { } public static class Server { - Server(ForgeConfigSpec.Builder builder) {} + public static ForgeConfigSpec.IntValue geologistPickDepositThreshold; + + Server(ForgeConfigSpec.Builder builder) { + builder.comment("Settings for geologist pick").push("geologist_pick"); + geologistPickDepositThreshold = builder + .comment("The ore block quantity needed for an ore to be recognised as a deposit.") + .defineInRange("geologistPickDepositThreshold", 500, 0, Integer.MAX_VALUE); + builder.pop(); + } private static void onConfigReload() {} } diff --git a/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java b/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java new file mode 100644 index 0000000..3fabe8e --- /dev/null +++ b/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java @@ -0,0 +1,132 @@ +package dev.micle.geologistpicktweaks.mixin; + +import com.igteam.immersivegeology.common.block.helper.IOreBlock; +import com.igteam.immersivegeology.common.item.IGGenericItem; +import com.igteam.immersivegeology.common.item.IGMineralTestingItem; +import com.igteam.immersivegeology.common.item.helper.IGFlagItem; +import com.igteam.immersivegeology.core.material.helper.flags.BlockCategoryFlags; +import com.igteam.immersivegeology.core.material.helper.flags.ItemCategoryFlags; +import com.igteam.immersivegeology.core.material.helper.material.MaterialInterface; +import dev.micle.geologistpicktweaks.config.Config; +import dev.micle.geologistpicktweaks.util.MineralCacheEntry; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.tags.TagKey; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.UseOnContext; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.LevelChunkSection; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; + +import java.util.*; +import java.util.stream.Collectors; + +@Mixin(IGMineralTestingItem.class) +public abstract class IGMineralTestingItemMixin extends IGGenericItem implements IGFlagItem { + @Unique + public final HashMap geologist_pick_tweaks_1_20_1$mineralCache = new HashMap<>(); + + public IGMineralTestingItemMixin(ItemCategoryFlags flag, MaterialInterface material) { + super(flag, material); + } + + /** + * @author Micle + * @reason Tweaking geologist pick usage logic. + */ + @Overwrite + public @NotNull InteractionResult useOn(UseOnContext context) { + Player player = context.getPlayer(); + ItemStack stack = context.getItemInHand(); + if (player == null || !stack.getItem().equals(this)) { + return InteractionResult.FAIL; + } + + Level level = context.getLevel(); + BlockPos usedPos = context.getClickedPos(); + ChunkPos centerChunkPos = new ChunkPos(usedPos); + + stack.hurtAndBreak(1, player, (p) -> {}); + + MineralCacheEntry cachedEntry = geologist_pick_tweaks_1_20_1$mineralCache.get(centerChunkPos); + if (cachedEntry != null) { + long currentTimestamp = System.currentTimeMillis(); + if ((currentTimestamp - cachedEntry.timestamp) > MineralCacheEntry.CACHE_EXPIRY) { + geologist_pick_tweaks_1_20_1$mineralCache.clear(); + } else { + player.displayClientMessage(Component.literal(cachedEntry.message), true); + return InteractionResult.SUCCESS; + } + } + + int centerChunkX = centerChunkPos.x; + int centerChunkZ = centerChunkPos.z; + int minBuildHeight = level.getMinBuildHeight(); + int maxBuildHeight = level.getMaxBuildHeight(); + int sectionMin = level.getSectionIndex(minBuildHeight); + int sectionMax = level.getSectionIndex(maxBuildHeight); + + HashMap, Integer> oreMap = new HashMap<>(); + TagKey allOresTag = BlockCategoryFlags.ORE_BLOCK.getCategoryTag(); + for (int dx = -1; dx <= 1; dx++) { + for (int dz = -1; dz <= 1; dz++) { + ChunkAccess chunk = level.getChunk(centerChunkX + dx, centerChunkZ + dz); + + for (int sectionIndex = sectionMin; sectionIndex < sectionMax; sectionIndex++) { + LevelChunkSection section = chunk.getSection(sectionIndex); + + if (section.hasOnlyAir()) { + continue; + } + + if (!section.maybeHas(b -> b.is(allOresTag))) { + continue; + } + + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + BlockState blockState = section.getBlockState(x, y, z); + if (blockState.is(allOresTag)) { + IOreBlock ore = (IOreBlock) blockState.getBlock(); + oreMap.put(ore.getOreMaterial(), oreMap.getOrDefault(ore.getOreMaterial(), 0) + 1); + } + } + } + } + } + } + } + + // Apply deposit threshold + oreMap.values().removeIf(value -> value < Config.Server.geologistPickDepositThreshold.get()); + + // Sort ore map + oreMap = oreMap.entrySet().stream() + .sorted((k1, k2) -> -k1.getValue().compareTo(k2.getValue())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); + + Component message = getMessage(oreMap.keySet()); + + player.displayClientMessage(message, true); + geologist_pick_tweaks_1_20_1$mineralCache.put(centerChunkPos, cachedEntry); + + return InteractionResult.SUCCESS; + } + + @Shadow + @NotNull + private static Component getMessage(Set> oreSet) { + return Component.empty(); + } +} diff --git a/src/main/java/dev/micle/geologistpicktweaks/util/MineralCacheEntry.java b/src/main/java/dev/micle/geologistpicktweaks/util/MineralCacheEntry.java new file mode 100644 index 0000000..d2cffc5 --- /dev/null +++ b/src/main/java/dev/micle/geologistpicktweaks/util/MineralCacheEntry.java @@ -0,0 +1,15 @@ +package dev.micle.geologistpicktweaks.util; + +/** + * @author muddykat + */ +public class MineralCacheEntry { + public static final long CACHE_EXPIRY = 10 * 1000; + public final String message; + public final long timestamp; + + public MineralCacheEntry(String message) { + this.message = message; + this.timestamp = System.currentTimeMillis(); + } +} diff --git a/src/main/resources/geologist_pick_tweaks.mixins.json b/src/main/resources/geologist_pick_tweaks.mixins.json index 12208a1..28d68ad 100644 --- a/src/main/resources/geologist_pick_tweaks.mixins.json +++ b/src/main/resources/geologist_pick_tweaks.mixins.json @@ -5,6 +5,7 @@ "compatibilityLevel": "JAVA_8", "refmap": "geologist_pick_tweaks.refmap.json", "mixins": [ + "IGMineralTestingItemMixin" ], "client": [ ],