Skip to content

Commit

Permalink
feat: allow divination rods to find blocks in same tag
Browse files Browse the repository at this point in the history
and fix not finding any results if a block does not have a deepslate equivalent
  • Loading branch information
klikli-dev committed Jun 1, 2023
1 parent c8ea822 commit 93e5171
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions src/main/java/com/klikli_dev/theurgy/item/DivinationRodItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.client.renderer.item.ItemPropertyFunction;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.*;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -37,6 +38,7 @@
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -48,6 +50,7 @@

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class DivinationRodItem extends Item {

Expand All @@ -74,6 +77,44 @@ public DivinationRodItem(Properties pProperties, Tier defaultTier, TagKey<Block>
this.defaultAllowAttuning = defaultAllowAttuning;
}

public static Set<Block> getScanTargetsForId(ResourceLocation linkedBlockId) {
//First: try to get a tag for the given block.
var tagKey = TagKey.create(Registry.BLOCK_REGISTRY, getOreTagFromBlockId(linkedBlockId));
var tag = ForgeRegistries.BLOCKS.tags().getTag(tagKey);

if (!tag.isEmpty())
return tag.stream().collect(Collectors.toSet());

//If no fitting tag succeeds, try to get block + deepslate variant
var block = ForgeRegistries.BLOCKS.getValue(linkedBlockId);
if (block != null) {
Block deepslateBlock = null;

//also search for deepslate ores
if (linkedBlockId.getPath().contains("_ore") && !linkedBlockId.getPath().contains("deepslate_")) {
var deepslateId = new ResourceLocation(linkedBlockId.getNamespace(), "deepslate_" + linkedBlockId.getPath());
deepslateBlock = ForgeRegistries.BLOCKS.getValue(deepslateId);
}

//finally, only add deepslate variant, if it is not air
return deepslateBlock != null && deepslateBlock != Blocks.AIR ? Set.of(block, deepslateBlock) : Set.of(block);
}

return Set.of();
}

public static ResourceLocation getOreTagFromBlockId(ResourceLocation blockId) {
var path = blockId.getPath();

String oreName = path
.replace("_ore", "")
.replace("ore_", "")
.replace("_deepslate", "")
.replace("deepslate_", "");

return new ResourceLocation("forge:ores/" + oreName);
}

@Override
public int getMaxDamage(ItemStack stack) {
return stack.getOrCreateTag().getInt(TheurgyConstants.Nbt.Divination.SETTING_DURABILITY);
Expand Down Expand Up @@ -185,23 +226,13 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera

if (level.isClientSide) {
var id = new ResourceLocation(stack.getTag().getString(TheurgyConstants.Nbt.Divination.LINKED_BLOCK_ID));
var block = ForgeRegistries.BLOCKS.getValue(id);
if (block != null) {
Block deepslateBlock = null;

//also search for deepslate ores
if (id.getPath().contains("_ore") && !id.getPath().contains("deepslate_")) {
var deepslateId = new ResourceLocation(id.getNamespace(), "deepslate_" + id.getPath());
deepslateBlock = ForgeRegistries.BLOCKS.getValue(deepslateId);
}

ScanManager.get().beginScan(player,
deepslateBlock != null ? Set.of(block, deepslateBlock) : Set.of(block),
tag.getInt(TheurgyConstants.Nbt.Divination.SETTING_RANGE),
tag.getInt(TheurgyConstants.Nbt.Divination.SETTING_DURATION)
);
}

var blocks = getScanTargetsForId(id);
ScanManager.get().beginScan(player,
blocks,
tag.getInt(TheurgyConstants.Nbt.Divination.SETTING_RANGE),
tag.getInt(TheurgyConstants.Nbt.Divination.SETTING_DURATION)
);
}
} else if (!level.isClientSide) {
player.sendMessage(
Expand All @@ -218,7 +249,7 @@ public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity enti
if (!(entityLiving instanceof Player player))
return stack;

if(stack.getDamageValue() >= stack.getMaxDamage()){
if (stack.getDamageValue() >= stack.getMaxDamage()) {
//if in the last usage cycle the item was used up, we now actually break it to avoid over-use
player.broadcastBreakEvent(player.getUsedItemHand());
var item = stack.getItem();
Expand Down Expand Up @@ -454,7 +485,7 @@ public static class DistHelper {
};

public static void fillItemCategory(DivinationRodItem item, CreativeModeTab tab, NonNullList<ItemStack> items) {
if(tab != Theurgy.CREATIVE_MODE_TAB && tab != CreativeModeTab.TAB_SEARCH)
if (tab != Theurgy.CREATIVE_MODE_TAB && tab != CreativeModeTab.TAB_SEARCH)
return;

var level = Minecraft.getInstance().level;
Expand Down

0 comments on commit 93e5171

Please sign in to comment.