-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add unbreakable carpet highlighter #1034
Open
Emirlol
wants to merge
7
commits into
SkyblockerMod:master
Choose a base branch
from
Emirlol:mining-additions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ff79a3
Add mithril carpet highlighter
Emirlol 35de39a
Make mithril carpet highlighter configurable
Emirlol d2c5240
Halve the required getBlockState calls
Emirlol 3b39a29
Add light blue carpet as a possible carpet block
Emirlol d7d4d50
Add tungsten carpets and rename carpet highlighter
Emirlol 07074b0
AVLTreeSet instead of ArraySet
Emirlol 1d42c14
Use scheduleCyclic instead of END_CLIENT_TICK event
Emirlol 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
This file contains 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 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
105 changes: 105 additions & 0 deletions
105
src/main/java/de/hysky/skyblocker/skyblock/dwarven/CarpetHighlighter.java
This file contains 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,105 @@ | ||
package de.hysky.skyblocker.skyblock.dwarven; | ||
|
||
import de.hysky.skyblocker.annotations.Init; | ||
import de.hysky.skyblocker.config.SkyblockerConfigManager; | ||
import de.hysky.skyblocker.events.SkyblockEvents; | ||
import de.hysky.skyblocker.utils.Boxes; | ||
import de.hysky.skyblocker.utils.Location; | ||
import de.hysky.skyblocker.utils.Resettable; | ||
import de.hysky.skyblocker.utils.render.RenderHelper; | ||
import de.hysky.skyblocker.utils.render.Renderable; | ||
import de.hysky.skyblocker.utils.scheduler.Scheduler; | ||
import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; | ||
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; | ||
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.block.CarpetBlock; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.Vec3d; | ||
|
||
import java.awt.*; | ||
|
||
/** | ||
* Highlights unbreakable carpets within ore veins in the Dwarven Mines. | ||
*/ | ||
public final class CarpetHighlighter implements Renderable, Resettable { | ||
public static final CarpetHighlighter INSTANCE = new CarpetHighlighter(); | ||
|
||
private static final Vec3d CARPET_BOUNDING_BOX = Boxes.getLengthVec(CarpetBlock.SHAPE.getBoundingBox()); | ||
private static final int SEARCH_RADIUS = 15; | ||
private static final int TICK_INTERVAL = 15; | ||
private static final ObjectAVLTreeSet<BlockPos> CARPET_LOCATIONS = new ObjectAVLTreeSet<>(); | ||
|
||
private static float[] colorComponents; | ||
private static int tickCounter = 0; | ||
private static boolean isLocationValid = false; | ||
|
||
@Init | ||
public static void init() { | ||
INSTANCE.configCallback(SkyblockerConfigManager.get().mining.dwarvenMines.carpetHighlightColor); | ||
WorldRenderEvents.AFTER_TRANSLUCENT.register(INSTANCE::render); | ||
SkyblockEvents.LOCATION_CHANGE.register(INSTANCE::onLocationChange); | ||
Scheduler.INSTANCE.scheduleCyclic(INSTANCE::tick, TICK_INTERVAL); | ||
ClientPlayConnectionEvents.JOIN.register(INSTANCE); | ||
} | ||
|
||
@Override | ||
public void render(WorldRenderContext context) { | ||
if (!isLocationValid || !SkyblockerConfigManager.get().mining.dwarvenMines.enableCarpetHighlighter) return; | ||
for (BlockPos carpetLocation : CARPET_LOCATIONS) { | ||
RenderHelper.renderFilled(context, carpetLocation, CARPET_BOUNDING_BOX, colorComponents, colorComponents[3], false); | ||
} | ||
} | ||
|
||
public void onLocationChange(Location location) { | ||
isLocationValid = location == Location.DWARVEN_MINES; | ||
} | ||
|
||
public void tick() { | ||
if (!isLocationValid || MinecraftClient.getInstance().world == null || MinecraftClient.getInstance().player == null) return; | ||
Iterable<BlockPos> iterable = BlockPos.iterateOutwards(MinecraftClient.getInstance().player.getBlockPos(), SEARCH_RADIUS, SEARCH_RADIUS, SEARCH_RADIUS); | ||
for (BlockPos blockPos : iterable) { | ||
//The iterator contains a BlockPos.Mutable that it changes the position of to iterate over blocks, | ||
// so it has to be converted to an immutable BlockPos or the position will change based on the player's position && the search radius | ||
if (checkForCarpet(blockPos)) CARPET_LOCATIONS.add(blockPos.toImmutable()); | ||
} | ||
} | ||
|
||
/** | ||
* @param blockPos The position to check for a carpet | ||
* @return Whether the block at the given position is a gray carpet with a sea lantern below it, which is how all unbreakable carpets are placed | ||
* @implNote <p>getBlockState is a heavy method, so this method will become a hot spot as the search radius increases || the tick interval decreases.</p> | ||
* <p>Consider profiling this method if either of those values are changed.</p> | ||
*/ | ||
private boolean checkForCarpet(BlockPos blockPos) { | ||
@SuppressWarnings("DataFlowIssue") // Null check is already done in the run method | ||
BlockState actualBlock = MinecraftClient.getInstance().world.getBlockState(blockPos); | ||
// Gray/light blue - mithril | ||
// Light gray - tungsten | ||
// There are other colors for some ores in the royal mines, | ||
// but since the actual ores don't include wool blocks | ||
// they're not easily confused as ores so they are not accounted for here | ||
if (!(actualBlock.isOf(Blocks.GRAY_CARPET) || | ||
actualBlock.isOf(Blocks.LIGHT_BLUE_CARPET) || | ||
actualBlock.isOf(Blocks.LIGHT_GRAY_CARPET))) return false; | ||
BlockState blockBelow = MinecraftClient.getInstance().world.getBlockState(blockPos.down()); | ||
return blockBelow.isOf(Blocks.SEA_LANTERN); | ||
} | ||
|
||
/** | ||
* <p>Caches the color components from the given color for rendering to avoid recalculating them every frame.</p> | ||
* <p>Called by the {@link de.hysky.skyblocker.config.categories.MiningCategory MiningCategory} > carpetHighlightColor when the color is updated.</p> | ||
*/ | ||
public void configCallback(Color color) { | ||
colorComponents = color.getRGBComponents(null); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
isLocationValid = false; | ||
CARPET_LOCATIONS.clear(); | ||
} | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also check for whether the feature is enabled or not so that we are not locating carpets unnecessarily.