Skip to content

Commit

Permalink
Update waypoints coordinate parsing and add dropdown scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Jun 24, 2024
1 parent e8a79f7 commit c1631ab
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Skyblock Mod 1.10.4 for 1.21

Support for 1.21
Support for 1.21.
Fix waypoints coordinates parsing and add default coordinates.
Add scrolling to dropdown.


## Skyblock Mod 1.10.3 for 1.20.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AbstractWaypointsScreen(Text title, T parent, Multimap<String, WaypointCa
protected void init() {
super.init();
waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 24));
islandWidget = addDrawableChild(new DropdownWidget<>(client, width - 160, 8, 150, Arrays.asList(Location.values()), this::islandChanged, Location.from(island)));
islandWidget = addDrawableChild(new DropdownWidget<>(client, width - 160, 8, 150, height - 8, Arrays.asList(Location.values()), this::islandChanged, Location.from(island)));
}

@Override
Expand All @@ -48,6 +48,14 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
return mouseClicked;
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
if (islandWidget.isMouseOver(mouseX, mouseY) && islandWidget.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount)) {
return true;
}
return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
}

protected void islandChanged(Location location) {
island = location.id();
waypointsListWidget.setIsland(island);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class DropdownWidget<T> extends ElementListWidget<DropdownWidget.Entry<T>
protected T selected;
protected boolean open;

public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, List<T> entries, Consumer<T> selectCallback, T selected) {
super(minecraftClient, width, (entries.size() + 1) * ENTRY_HEIGHT + 8, y, ENTRY_HEIGHT);
public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, int maxHeight, List<T> entries, Consumer<T> selectCallback, T selected) {
super(minecraftClient, width, Math.min((entries.size() + 1) * ENTRY_HEIGHT + 8, maxHeight), y, ENTRY_HEIGHT);
setX(x);
this.entries = entries;
this.selectCallback = selectCallback;
Expand Down Expand Up @@ -58,29 +58,44 @@ public void renderWidget(DrawContext context, int mouseX, int mouseY, float delt
context.getMatrices().push();
context.getMatrices().translate(0, 0, 100);

context.fill(getX(), getY(), getX() + width, getY() + headerHeight, 0xFF000000);
context.drawHorizontalLine(getX(), getX() + width, getY(), 0xFFFFFFFF);
context.drawHorizontalLine(getX(), getX() + width, getY() + headerHeight, 0xFFFFFFFF);
context.drawVerticalLine(getX(), getY(), getY() + headerHeight, 0xFFFFFFFF);
context.drawVerticalLine(getX() + width, getY(), getY() + headerHeight, 0xFFFFFFFF);
int y = getY() - (int) getScrollAmount();
int height = getMaxPosition();

context.fill(getX(), y, getX() + width, y + headerHeight, 0xFF000000);
context.drawHorizontalLine(getX(), getX() + width, y, 0xFFFFFFFF);
context.drawHorizontalLine(getX(), getX() + width, y + headerHeight, 0xFFFFFFFF);
context.drawVerticalLine(getX(), y, y + headerHeight, 0xFFFFFFFF);
context.drawVerticalLine(getX() + width, y, y + headerHeight, 0xFFFFFFFF);

if (open) {
context.fill(getX(), getY() + headerHeight + 1, getX() + width, getY() + height, 0xFF000000);
context.drawHorizontalLine(getX(), getX() + width, getY() + height, 0xFFFFFFFF);
context.drawVerticalLine(getX(), getY() + headerHeight, getY() + height, 0xFFFFFFFF);
context.drawVerticalLine(getX() + width, getY() + headerHeight, getY() + height, 0xFFFFFFFF);
context.fill(getX(), y + headerHeight + 1, getX() + width, y + height, 0xFF000000);
context.drawHorizontalLine(getX(), getX() + width, y + height, 0xFFFFFFFF);
context.drawVerticalLine(getX(), y + headerHeight, y + height, 0xFFFFFFFF);
context.drawVerticalLine(getX() + width, y + headerHeight, y + height, 0xFFFFFFFF);

super.renderWidget(context, mouseX, mouseY, delta);
} else {
renderHeader(context, getRowLeft(), getY() + 4 - (int) getScrollAmount());
renderHeader(context, getRowLeft(), y + 4);
}

context.getMatrices().pop();
}

@Override
protected void drawMenuListBackground(DrawContext context) {}

@Override
protected void drawHeaderAndFooterSeparators(DrawContext context) {}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
return open && super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
}

protected void select(T entry) {
selected = entry;
open = false;
setScrollAmount(0);
if (selected != prevSelected) {
selectCallback.accept(entry);
prevSelected = selected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.widget.*;
import net.minecraft.text.Text;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;

import java.util.ArrayList;
Expand Down Expand Up @@ -89,6 +91,10 @@ void updateButtons() {
}
}

private BlockPos getDefaultPos() {
return client.crosshairTarget instanceof BlockHitResult blockHitResult && client.crosshairTarget.getType() == HitResult.Type.BLOCK ? blockHitResult.getBlockPos() : client.player != null ? client.player.getBlockPos() : BlockPos.ORIGIN;
}

protected abstract static class AbstractWaypointEntry extends ElementListWidget.Entry<AbstractWaypointEntry> {
}

Expand Down Expand Up @@ -178,7 +184,7 @@ protected class WaypointEntry extends AbstractWaypointEntry {
private final ButtonWidget buttonDelete;

public WaypointEntry(WaypointCategoryEntry category) {
this(category, new NamedWaypoint(BlockPos.ORIGIN, "New Waypoint", new float[]{0f, 1f, 0f}));
this(category, new NamedWaypoint(getDefaultPos(), "New Waypoint", new float[]{0f, 1f, 0f}));
}

public WaypointEntry(WaypointCategoryEntry category, NamedWaypoint waypoint) {
Expand All @@ -190,12 +196,15 @@ public WaypointEntry(WaypointCategoryEntry category, NamedWaypoint waypoint) {
nameField.setChangedListener(this::updateName);
xField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("X"));
xField.setText(Integer.toString(waypoint.pos.getX()));
xField.setTextPredicate(this::checkInt);
xField.setChangedListener(this::updateX);
yField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("Y"));
yField.setText(Integer.toString(waypoint.pos.getY()));
yField.setTextPredicate(this::checkInt);
yField.setChangedListener(this::updateY);
zField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("Z"));
zField.setText(Integer.toString(waypoint.pos.getZ()));
zField.setTextPredicate(this::checkInt);
zField.setChangedListener(this::updateZ);
colorField = new TextFieldWidget(client.textRenderer, 56, 20, Text.literal("Color"));
colorField.setText(String.format("%02X%02X%02X%02X", (int) (waypoint.alpha * 255), (int) (waypoint.getColorComponents()[0] * 255), (int) (waypoint.getColorComponents()[1] * 255), (int) (waypoint.getColorComponents()[2] * 255)));
Expand All @@ -217,7 +226,7 @@ public List<? extends Element> children() {
return children;
}

public void updateName(String name) {
private void updateName(String name) {
if (waypoint.name.getString().equals(name)) return;
int index = category.category.waypoints().indexOf(waypoint);
waypoint = waypoint.withName(name);
Expand All @@ -226,22 +235,31 @@ public void updateName(String name) {
}
}

public void updateX(String xString) {
private boolean checkInt(String string) {
try {
parseEmptiableInt(string);
return true;
} catch (NumberFormatException e) {
return false;
}
}

private void updateX(String xString) {
updateInt(xString, waypoint.pos.getX(), waypoint::withX);
}

public void updateY(String yString) {
private void updateY(String yString) {
updateInt(yString, waypoint.pos.getY(), waypoint::withY);
}

public void updateZ(String zString) {
private void updateZ(String zString) {
updateInt(zString, waypoint.pos.getZ(), waypoint::withZ);
}

public void updateInt(String newValueString, int currentValue, Int2ObjectFunction<NamedWaypoint> wither) {
private void updateInt(String newValueString, int currentValue, Int2ObjectFunction<NamedWaypoint> wither) {
try {
int index = category.category.waypoints().indexOf(waypoint);
int newValue = Integer.parseInt(newValueString);
int newValue = parseEmptiableInt(newValueString);
if (newValue == currentValue) return;
waypoint = wither.apply(newValue);
if (index >= 0) {
Expand All @@ -252,10 +270,10 @@ public void updateInt(String newValueString, int currentValue, Int2ObjectFunctio
}
}

public void updateColor(String colorString) {
private void updateColor(String colorString) {
try {
int index = category.category.waypoints().indexOf(waypoint);
int colorInt = Integer.parseInt(colorString, 16);
int colorInt = parseEmptiableInt(colorString, 16);
float[] colorComponents = {((colorInt & 0x00FF0000) >> 16) / 255f, ((colorInt & 0x0000FF00) >> 8) / 255f, (colorInt & 0x000000FF) / 255f};
float alpha = ((colorInt & 0xFF000000) >>> 24) / 255f;
if (Arrays.equals(waypoint.getColorComponents(), colorComponents) && waypoint.alpha == alpha) return;
Expand All @@ -268,6 +286,15 @@ public void updateColor(String colorString) {
}
}

private int parseEmptiableInt(String value) throws NumberFormatException {
return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value);
}

@SuppressWarnings("SameParameterValue")
private int parseEmptiableInt(String value, int radix) throws NumberFormatException {
return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value, radix);
}

@Override
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
context.drawTextWithShadow(client.textRenderer, "X:", width / 2 - 56, y + 6, 0xFFFFFF);
Expand Down

0 comments on commit c1631ab

Please sign in to comment.