|
|
|
@ -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<ChunkPos, MineralCacheEntry> 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<MaterialInterface<?>, Integer> oreMap = new HashMap<>();
|
|
|
|
|
TagKey<Block> 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<MaterialInterface<?>> oreSet) {
|
|
|
|
|
return Component.empty();
|
|
|
|
|
}
|
|
|
|
|
}
|