Skip to content

Commit

Permalink
Fix bugs.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <[email protected]>
  • Loading branch information
qyl27 committed Oct 26, 2024
1 parent 45898eb commit b245abf
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 26 deletions.
12 changes: 8 additions & 4 deletions src/main/java/net/kyrptonaught/diggusmaximus/Excavate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.kyrptonaught.diggusmaximus.config.ConfigHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -16,6 +18,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Optional;

public class Excavate {
private final BlockPos startPos;
Expand All @@ -25,7 +28,7 @@ public class Excavate {
private int mined = 0;
private final Level level;

private final BlockState startBlock;
private final Optional<Holder.Reference<Block>> startBlock;
private final Deque<BlockPos> points = new ArrayDeque<>();

private final Direction facing;
Expand All @@ -37,7 +40,7 @@ public Excavate(BlockPos pos, ResourceLocation startId, Player player, Direction
this.level = player.getCommandSenderWorld();
this.startId = ResourceKey.create(Registries.BLOCK, startId);

this.startBlock = this.level.getBlockState(pos);
this.startBlock = BuiltInRegistries.BLOCK.get(startId);

this.startTool = player.getMainHandItem().getItem();
this.facing = facing;
Expand All @@ -46,7 +49,8 @@ public Excavate(BlockPos pos, ResourceLocation startId, Player player, Direction
public void startExcavate(int shapeSelection) {
this.shapeSelection = shapeSelection;
forceExcavateAt(startPos);
if (startBlock.is(startId) && ExcavateHelper.isBlockBlocked(startBlock)) {
if (startBlock.isEmpty()
|| (startBlock.orElseThrow().is(startId) && ExcavateHelper.isBlockBlocked(startBlock.orElseThrow()))) {
return;
}

Expand All @@ -71,7 +75,7 @@ private void excavateAt(BlockPos pos) {
}
var block = ExcavateHelper.getBlockAt(level, pos);
if (!block.isAir()
&& ExcavateHelper.isTheSameBlock(startBlock, block, shapeSelection)
&& ExcavateHelper.isTheSameBlock(startBlock.orElseThrow(), block, shapeSelection)
&& ExcavateHelper.canMine(player, startTool, level, startPos, pos)
&& isExcavatingAllowed(pos)) {
forceExcavateAt(pos);
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/net/kyrptonaught/diggusmaximus/ExcavateHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import net.kyrptonaught.diggusmaximus.config.ConfigHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.item.ItemEntity;
Expand All @@ -27,7 +28,7 @@ public static void pickupDrops(Level world, BlockPos pos, Player player) {
});
}

public static boolean isTheSameBlock(BlockState original, BlockState newBlock, int shapeSelection) {
public static boolean isTheSameBlock(Holder.Reference<Block> original, BlockState newBlock, int shapeSelection) {
if (shapeSelection > -1 && ConfigHelper.getConfig().shapes.includeDifBlocks) {
return true;
}
Expand All @@ -52,10 +53,10 @@ public static boolean isTheSameBlock(BlockState original, BlockState newBlock, i
}
}

return original.is(newBlock.getBlock());
return original.is(newBlock.getBlockHolder());
}

public static boolean isBlockBlocked(BlockState block) {
public static boolean isBlockBlocked(Holder.Reference<Block> block) {
var config = ConfigHelper.getConfig().blockList;
if (config.isWhitelist) {
for (var e : config.blocked) {
Expand Down Expand Up @@ -114,6 +115,17 @@ private static boolean checkTool(Player player, Item tool) {
}

private static boolean isTool(ItemStack stack) {
return stack.isDamageableItem() || ConfigHelper.getConfig().config.tools.contains(BuiltInRegistries.ITEM.getKey(stack.getItem()).toString());
if (stack.isDamageableItem()) {
return true;
}

for (var e : ConfigHelper.getConfig().config.customTools) {
var v = e.map(l -> stack.getItemHolder().is(l), stack::is);
if (v) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.datafixers.util.Either;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigHolder;
import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer;
import me.shedaniel.autoconfig.serializer.PartitioningSerializer;
import net.minecraft.client.Minecraft;
Expand All @@ -10,6 +11,8 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

public class ConfigHelper {
Expand All @@ -27,8 +30,18 @@ public static void registerConfig() {
AutoConfig.register(ModConfig.class, PartitioningSerializer.wrap(JanksonConfigSerializer::new));
}

private static ConfigHolder<ModConfig> holder;

public static ModConfig getConfig() {
return AutoConfig.getConfigHolder(ModConfig.class).get();
if (holder == null) {
holder = AutoConfig.getConfigHolder(ModConfig.class);
holder.registerSaveListener((holder, config) -> {
config.blockList.update();
config.grouping.update();
return InteractionResult.SUCCESS;
});
}
return holder.get();
}

public static Either<ResourceKey<Block>, TagKey<Block>> parseBlockOrTag(String s) {
Expand All @@ -42,4 +55,16 @@ public static Either<ResourceKey<Block>, TagKey<Block>> parseBlockOrTag(String s
return Either.left(rk);
}
}

public static Either<ResourceKey<Item>, TagKey<Item>> parseItemOrTag(String s) {
if (s.startsWith("#")) {
var rl = ResourceLocation.parse(s.substring(1));
var key = TagKey.create(Registries.ITEM, rl);
return Either.right(key);
} else {
var rl = ResourceLocation.parse(s);
var rk = ResourceKey.create(Registries.ITEM, rl);
return Either.left(rk);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import net.kyrptonaught.diggusmaximus.ModConstants;
import net.kyrptonaught.diggusmaximus.config.category.BlockGroups;
import net.kyrptonaught.diggusmaximus.config.category.BlockList;
import net.kyrptonaught.diggusmaximus.config.category.ConfigOptions;
import net.kyrptonaught.diggusmaximus.config.category.Options;
import net.kyrptonaught.diggusmaximus.config.category.ExcavatingShapes;

@Config(name = ModConstants.MOD_ID)
public class ModConfig extends PartitioningSerializer.GlobalData {
@ConfigEntry.Category("config")
@ConfigEntry.Gui.TransitiveObject
public ConfigOptions config = new ConfigOptions();
public Options config = new Options();

@ConfigEntry.Category("blacklist")
@ConfigEntry.Gui.TransitiveObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ public class BlockGroups implements ConfigData {

@Override
public void validatePostLoad() throws ValidationException {
ConfigData.super.validatePostLoad();
blockGroups.clear();

try {
for (var g : groups) {
var set = new HashSet<Either<ResourceKey<Block>, TagKey<Block>>>();
for (var s : g.split(",")) {
set.add(ConfigHelper.parseBlockOrTag(s));
}
blockGroups.add(set);
}
update();
} catch (Exception ex) {
throw new ValidationException(ex);
}
}

public void update() {
blockGroups.clear();

for (var g : groups) {
var set = new HashSet<Either<ResourceKey<Block>, TagKey<Block>>>();
for (var s : g.split(",")) {
set.add(ConfigHelper.parseBlockOrTag(s));
}
blockGroups.add(set);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ public void validatePostLoad() throws ValidationException {
blocked.clear();

try {
for (var s : blacklistedBlocks) {
blocked.add(ConfigHelper.parseBlockOrTag(s));
}
update();
} catch (Exception ex) {
throw new ValidationException(ex);
}
}

public void update() {
blocked.clear();

for (var s : blacklistedBlocks) {
blocked.add(ConfigHelper.parseBlockOrTag(s));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
import net.kyrptonaught.diggusmaximus.ExcavateTypes;

Expand All @@ -14,5 +15,8 @@ public class ExcavatingShapes implements ConfigData {
public boolean includeDifBlocks = false;

@Comment("Currently selected shape")
@ConfigEntry.Gui.EnumHandler(
option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON
)
public ExcavateTypes.Shape selectedShape = ExcavateTypes.Shape.LAYER;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package net.kyrptonaught.diggusmaximus.config.category;

import com.mojang.datafixers.util.Either;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
import net.kyrptonaught.diggusmaximus.config.ConfigHelper;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Config(name = "blacklist")
public class ConfigOptions implements ConfigData {
@Config(name = "config")
public class Options implements ConfigData {

@Comment("Mod enabled or disabled")
public boolean enabled = true;
Expand Down Expand Up @@ -54,4 +62,23 @@ public class ConfigOptions implements ConfigData {

@Comment("Other items to be considered tools ie: \"minecraft:stick\"")
public List<String> tools = new ArrayList<>();

public final Set<Either<ResourceKey<Item>, TagKey<Item>>> customTools = new HashSet<>();

@Override
public void validatePostLoad() throws ValidationException {
try {
update();
} catch (Exception ex) {
throw new ValidationException(ex);
}
}

public void update() {
customTools.clear();

for (var s : tools) {
customTools.add(ConfigHelper.parseItemOrTag(s));
}
}
}

0 comments on commit b245abf

Please sign in to comment.