-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor mining speed logic into common modules #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c3b756
refactor(common,1.20.1-common,1.20.1-forge,26.1-common,26.1-neo): mov…
Meatwo310 aadfc27
refactor(common,1.20.1-common,1.20.1-forge,26.1-common,26.1-neo): int…
Meatwo310 63e1cdf
refactor(common,1.20.1-common,1.20.1-forge,26.1-common,26.1-neo): add…
Meatwo310 4b90c70
refactor(common,1.20.1-common,1.20.1-forge,26.1-neo): simplify config…
Meatwo310 4461b62
fix(1.20.1-forge): avoid duplicate tag warning
Meatwo310 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
1.20.1-common/src/main/java/net/meatwo310/softdeepslate/SoftDeepslateLogic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package net.meatwo310.softdeepslate; | ||
|
|
||
| import net.meatwo310.softdeepslate.config.ModServerConfig; | ||
| import net.minecraft.resources.ResourceLocation; | ||
| import net.minecraft.world.level.block.Block; | ||
| import net.minecraft.world.level.block.state.BlockState; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| public final class SoftDeepslateLogic { | ||
| private final ModServerConfig config; | ||
| private final BlockResolver blockResolver; | ||
| private volatile Set<Block> blocksCache; | ||
|
|
||
| public SoftDeepslateLogic(ModServerConfig config, BlockResolver blockResolver) { | ||
| this.config = Objects.requireNonNull(config); | ||
| this.blockResolver = Objects.requireNonNull(blockResolver); | ||
| } | ||
|
|
||
| public boolean shouldMineFaster(BlockState state) { | ||
| return getBlocks().contains(state.getBlock()); | ||
| } | ||
|
|
||
| public double miningSpeed() { | ||
| return config.miningSpeed(); | ||
| } | ||
|
|
||
| public void invalidateBlockCache() { | ||
| blocksCache = null; | ||
| } | ||
|
|
||
| private Set<Block> getBlocks() { | ||
| Set<Block> cached = blocksCache; | ||
| if (cached == null) { | ||
| synchronized (this) { | ||
| cached = blocksCache; | ||
| if (cached == null) { | ||
| cached = buildCache(); | ||
| blocksCache = cached; | ||
| } | ||
| } | ||
| } | ||
| return cached; | ||
| } | ||
|
|
||
| private Set<Block> buildCache() { | ||
| HashSet<Block> blocks = new HashSet<>(); | ||
|
|
||
| for (String name : config.blocks()) { | ||
| if (name.startsWith("#")) { | ||
| cacheBlockTag(blocks, name.substring(1)); | ||
| } else { | ||
| cacheBlock(blocks, name); | ||
| } | ||
| } | ||
|
|
||
| if (blocks.isEmpty()) { | ||
| Constants.LOGGER.warn("No valid blocks found"); | ||
| } else { | ||
| Constants.LOGGER.info("Cached {} blocks", blocks.size()); | ||
| } | ||
|
|
||
| return Set.copyOf(blocks); | ||
| } | ||
|
|
||
| private void cacheBlock(Set<Block> blocks, String blockName) { | ||
| ResourceLocation id = ResourceLocation.tryParse(blockName); | ||
| if (id == null) { | ||
| return; | ||
| } | ||
|
|
||
| blockResolver.resolveBlock(id).ifPresentOrElse( | ||
| block -> { | ||
| Constants.LOGGER.debug("Caching block: {}{}", blockName, blocks.contains(block) ? " (DUPLICATED)" : ""); | ||
| blocks.add(block); | ||
| }, | ||
| () -> Constants.LOGGER.warn("Unknown block in config: {}", blockName) | ||
| ); | ||
| } | ||
|
|
||
| private void cacheBlockTag(Set<Block> blocks, String blockTagName) { | ||
| ResourceLocation id = ResourceLocation.tryParse(blockTagName); | ||
| if (id == null) { | ||
| return; | ||
| } | ||
|
|
||
| blockResolver.resolveTag(id).ifPresentOrElse( | ||
| taggedBlocks -> { | ||
| for (Block block : taggedBlocks) { | ||
| Constants.LOGGER.debug( | ||
| "Caching block from tag #{}: {}{}", | ||
| blockTagName, | ||
| blockResolver.blockName(block), | ||
| blocks.contains(block) ? " (DUPLICATED)" : "" | ||
| ); | ||
| blocks.add(block); | ||
| } | ||
| }, | ||
| () -> Constants.LOGGER.warn("Unknown block tag in config: #{}", blockTagName) | ||
| ); | ||
| } | ||
|
|
||
| public interface BlockResolver { | ||
| Optional<Block> resolveBlock(ResourceLocation id); | ||
| Optional<? extends Iterable<Block>> resolveTag(ResourceLocation id); | ||
| String blockName(Block block); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.