diff --git a/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java b/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java index 54ea54d..69335db 100644 --- a/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java +++ b/src/main/java/dev/micle/geologistpicktweaks/mixin/IGMineralTestingItemMixin.java @@ -1,22 +1,145 @@ 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 dev.micle.geologistpicktweaks.GeologistPickTweaks; +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.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.Unique; + +import java.util.*; @Mixin(IGMineralTestingItem.class) -public abstract class IGMineralTestingItemMixin { +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 Improving geologist pick usage logic. + * @reason Tweaking geologist pick usage logic. */ @Overwrite public InteractionResult useOn(UseOnContext context) { - GeologistPickTweaks.LOGGER.debug("HELLO WORLD"); + 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); + + Set> oreSet = new HashSet<>(); + TagKey allOresTag = BlockCategoryFlags.ORE_BLOCK.getCategoryTag(); + chunkScan: 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(); + oreSet.add(ore.getOreMaterial()); + if (oreSet.size() >= 3) { + break chunkScan; + } + } + } + } + } + } + } + } + + Component message = geologist_pick_tweaks_1_20_1$getMessage(oreSet); + + player.displayClientMessage(message, true); + geologist_pick_tweaks_1_20_1$mineralCache.put(centerChunkPos, cachedEntry); return InteractionResult.SUCCESS; } + + @Unique + private static @NotNull Component geologist_pick_tweaks_1_20_1$getMessage(Set> oreSet) { + Component message; + if (oreSet.isEmpty()) { + message = Component.translatable("immersivegeology.prospecting_pick.nothing"); + } else { + List> found = new ArrayList<>(oreSet); + String messageKey = "immersivegeology.prospecting_pick.found"; + String materialsText; + + switch (found.size()) { + case 1: + materialsText = "Found Traces of " + found.get(0); + break; + case 2: + materialsText = "Found Traces of " + found.get(0) + " and " + found.get(1); + break; + default: // 3 or more + materialsText = "Found Cluster of " + found.get(0) + ", " + found.get(1) + " and " + found.get(2); + break; + } + + message = Component.translatable(messageKey, materialsText); + } + return message; + } } 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(); + } +}