Skip to content

Commit e0e5f82

Browse files
committed
feat: added claim modes in chunk claiming screen
freehand (default)/rectangle/circle
1 parent a62e4c1 commit e0e5f82

File tree

6 files changed

+109
-27
lines changed

6 files changed

+109
-27
lines changed

common/src/main/java/dev/ftb/mods/ftbchunks/client/gui/ChunkScreen.java

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package dev.ftb.mods.ftbchunks.client.gui;
22

33
import dev.ftb.mods.ftbchunks.FTBChunks;
4+
import dev.ftb.mods.ftbchunks.api.FTBChunksAPI;
45
import dev.ftb.mods.ftbchunks.client.FTBChunksClient;
56
import dev.ftb.mods.ftbchunks.client.map.MapDimension;
67
import dev.ftb.mods.ftbchunks.net.SendGeneralDataPacket;
78
import dev.ftb.mods.ftblibrary.icon.Color4I;
9+
import dev.ftb.mods.ftblibrary.icon.Icon;
810
import dev.ftb.mods.ftblibrary.icon.Icons;
9-
import dev.ftb.mods.ftblibrary.ui.Button;
10-
import dev.ftb.mods.ftblibrary.ui.Panel;
11-
import dev.ftb.mods.ftblibrary.ui.SimpleButton;
12-
import dev.ftb.mods.ftblibrary.ui.Theme;
13-
import dev.ftb.mods.ftblibrary.ui.ToggleableButton;
11+
import dev.ftb.mods.ftblibrary.ui.*;
1412
import dev.ftb.mods.ftblibrary.ui.misc.AbstractThreePanelScreen;
1513
import dev.ftb.mods.ftblibrary.ui.misc.KeyReferenceScreen;
1614
import dev.ftb.mods.ftblibrary.util.TooltipList;
@@ -30,7 +28,9 @@ public class ChunkScreen extends AbstractThreePanelScreen<ChunkScreenPanel> {
3028
private ChunkScreenPanel chunkScreenPanel;
3129
private SimpleButton largeMapButton;
3230

33-
private ChunkScreen(MapDimension dimension, Team openedAs) {
31+
static ClaimMode claimMode = ClaimMode.FREEHAND; // persist across invocations
32+
33+
private ChunkScreen(MapDimension dimension, @Nullable Team openedAs) {
3434
this.dimension = dimension;
3535
this.openedAs = openedAs;
3636
showCloseButton(true);
@@ -49,10 +49,10 @@ protected int getScrollbarWidth() {
4949

5050
@Override
5151
public boolean onInit() {
52-
int size = (int) (getScreen().getGuiScaledHeight() * 0.85f);
52+
int size = (int) (getWindow().getGuiScaledHeight() * 0.85f);
5353

54-
setWidth(Math.min(size + 2, getScreen().getGuiScaledWidth() - 2));
55-
setHeight(Math.min(size + getTopPanelHeight() + getBottomPanelHeight(), getScreen().getGuiScaledHeight() - 2));
54+
setWidth(Math.min(size + 2, getWindow().getGuiScaledWidth() - 2));
55+
setHeight(Math.min(size + getTopPanelHeight() + getBottomPanelHeight(), getWindow().getGuiScaledHeight() - 2));
5656

5757
return true;
5858
}
@@ -133,6 +133,7 @@ protected class CustomTopPanel extends Panel {
133133
private final Button removeAllClaims;
134134
private final Button adminButton;
135135
private final Button mouseReferenceButton;
136+
private final Button claimModeButton;
136137

137138
public CustomTopPanel() {
138139
super(ChunkScreen.this);
@@ -158,6 +159,8 @@ public CustomTopPanel() {
158159
.setForceButtonSize(false);
159160

160161
adminButton = new AdminButton().setForceButtonSize(false);
162+
163+
claimModeButton = new ClaimModeButton();
161164
}
162165

163166
@Override
@@ -168,6 +171,7 @@ public void addWidgets() {
168171
add(adminButton);
169172
}
170173
add(mouseReferenceButton);
174+
add(claimModeButton);
171175
}
172176

173177
@Override
@@ -177,7 +181,7 @@ public void alignWidgets() {
177181

178182
closeButton.setPosAndSize(width - 16, 2, 12, 12);
179183
mouseReferenceButton.setPosAndSize(width - 32, 2, 12, 12);
180-
184+
claimModeButton.setPosAndSize(width - 48, 2, 12, 12);
181185
}
182186

183187
@Override
@@ -206,6 +210,16 @@ public void addMouseOverText(TooltipList list) {
206210
list.add(MORE_INFO);
207211
}
208212
}
213+
214+
private class ClaimModeButton extends SimpleButton {
215+
public ClaimModeButton() {
216+
super(CustomTopPanel.this, claimMode.description(), claimMode.icon(), (widget, button) -> {
217+
claimMode = claimMode.next();
218+
widget.setIcon(claimMode.icon());
219+
widget.setTitle(claimMode.description());
220+
});
221+
}
222+
}
209223
}
210224

211225
private static class ChunkMouseReferenceScreen extends KeyReferenceScreen {
@@ -220,7 +234,6 @@ public Component getTitle() {
220234
}
221235

222236
private class CustomBottomPanel extends Panel {
223-
224237
public CustomBottomPanel() {
225238
super(ChunkScreen.this);
226239
}
@@ -266,4 +279,30 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
266279
}
267280
}
268281
}
282+
283+
enum ClaimMode {
284+
FREEHAND("freehand", Icon.getIcon(FTBChunksAPI.rl("textures/freehand.png"))),
285+
RECTANGLE("rectangle", Icon.getIcon(FTBChunksAPI.rl("textures/rectangle.png"))),
286+
CIRCLE("circle", Icon.getIcon(FTBChunksAPI.rl("textures/circle.png")));
287+
288+
private final String key;
289+
private final Icon icon;
290+
291+
ClaimMode(String key, Icon icon) {
292+
this.key = key;
293+
this.icon = icon;
294+
}
295+
296+
Component description() {
297+
return Component.translatable("ftbchunks.claim_mode." + key);
298+
}
299+
300+
public Icon icon() {
301+
return icon;
302+
}
303+
304+
public ClaimMode next() {
305+
return ClaimMode.values()[(ordinal() + 1) % ClaimMode.values().length];
306+
}
307+
}
269308
}

common/src/main/java/dev/ftb/mods/ftbchunks/client/gui/ChunkScreenPanel.java

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import net.minecraft.commands.Commands;
4040
import net.minecraft.network.chat.Component;
4141
import net.minecraft.network.chat.MutableComponent;
42+
import net.minecraft.util.Mth;
4243
import net.minecraft.world.entity.player.Player;
4344
import net.minecraft.world.level.ChunkPos;
4445
import org.lwjgl.glfw.GLFW;
@@ -60,6 +61,7 @@ public class ChunkScreenPanel extends Panel {
6061
private static final ImageIcon CHECKERED_ICON = new ImageIcon(FTBChunksAPI.rl("textures/checkered.png"));
6162

6263
private final List<ChunkButton> chunkButtons = new ArrayList<>();
64+
private XZ firstSelectedChunk = null;
6365
private final Set<XZ> selectedChunks = new HashSet<>();
6466
private final List<ChunkButtonPos> chunkedPosList = new ArrayList<>();
6567
public boolean isAdminEnabled;
@@ -129,11 +131,11 @@ public void alignWidgets() {
129131

130132
this.tileSizeX = maxWidth / FTBChunks.TILES;
131133
this.tileSizeY = maxHeight / FTBChunks.TILES;
132-
for (ChunkButtonPos chunkedPos : chunkedPosList) {
133-
chunkedPos.button.setPos(xPos + tileSizeX * chunkedPos.x, yPos + tileSizeY * chunkedPos.y);
134-
chunkedPos.button.setSize(tileSizeX, tileSizeY);
135-
}
136-
}
134+
for (ChunkButtonPos chunkedPos : chunkedPosList) {
135+
chunkedPos.button.setPos(xPos + tileSizeX * chunkedPos.x, yPos + tileSizeY * chunkedPos.y);
136+
chunkedPos.button.setSize(tileSizeX, tileSizeY);
137+
}
138+
}
137139

138140
@Override
139141
public void mouseReleased(MouseButton button) {
@@ -143,16 +145,17 @@ public void mouseReleased(MouseButton button) {
143145
Optional<UUID> teamId = Optional.ofNullable(chunkScreen.getOpenedAs()).map(Team::getTeamId);
144146
NetworkManager.sendToServer(new RequestChunkChangePacket(ChunkChangeOp.create(button.isLeft(), isShiftKeyDown()), selectedChunks, canChangeAsAdmin(), teamId));
145147
selectedChunks.clear();
148+
firstSelectedChunk = null;
146149
playClickSound();
147150
}
148151
}
149152

150153
public void removeAllClaims() {
151154
Optional<UUID> teamId = Optional.ofNullable(chunkScreen.getOpenedAs()).map(Team::getTeamId);
152155
Set<XZ> allChunks = chunkedPosList.stream()
153-
.map(ChunkButtonPos::button)
154-
.map(ChunkButton::getChunkPos)
155-
.collect(Collectors.toSet());
156+
.map(ChunkButtonPos::button)
157+
.map(ChunkButton::getChunkPos)
158+
.collect(Collectors.toSet());
156159
NetworkManager.sendToServer(new RequestChunkChangePacket(ChunkChangeOp.UNCLAIM, allChunks, canChangeAsAdmin(), teamId));
157160
}
158161

@@ -226,6 +229,10 @@ public ChunkButton(Panel panel, XZ xz) {
226229

227230
@Override
228231
public void onClicked(MouseButton mouseButton) {
232+
if (selectedChunks.isEmpty()) {
233+
firstSelectedChunk = chunkPos;
234+
}
235+
229236
selectedChunks.add(chunkPos);
230237
}
231238

@@ -248,12 +255,39 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
248255

249256
@Override
250257
public boolean mouseDragged(int button, double dragX, double dragY) {
251-
if (isMouseOver() && (isMouseButtonDown(MouseButton.LEFT) || isMouseButtonDown(MouseButton.RIGHT))) {
252-
selectedChunks.add(chunkPos);
253-
}
258+
if (isMouseOver() && (isMouseButtonDown(MouseButton.LEFT) || isMouseButtonDown(MouseButton.RIGHT))) {
259+
if (ChunkScreen.claimMode != ChunkScreen.ClaimMode.FREEHAND && firstSelectedChunk != null) {
260+
addChunksToSelection();
261+
} else {
262+
selectedChunks.add(chunkPos);
263+
}
264+
}
254265
return super.mouseDragged(button, dragX, dragY);
255266
}
256267

268+
private void addChunksToSelection() {
269+
int x1 = Math.min(firstSelectedChunk.x(), chunkPos.x());
270+
int x2 = Math.max(firstSelectedChunk.x(), chunkPos.x());
271+
int z1 = Math.min(firstSelectedChunk.z(), chunkPos.z());
272+
int z2 = Math.max(firstSelectedChunk.z(), chunkPos.z());
273+
XZ centre = new XZ((x1 + x2) / 2, (z1 + z2) / 2);
274+
selectedChunks.clear();
275+
for (int x = x1; x <= x2; x++) {
276+
for (int z = z1; z <= z2; z++) {
277+
XZ xz = new XZ(x, z);
278+
if (ChunkScreen.claimMode == ChunkScreen.ClaimMode.RECTANGLE
279+
|| distance(xz, centre) - 0.5f <= Math.min(centre.x() - x1, centre.z() - z1))
280+
{
281+
selectedChunks.add(xz);
282+
}
283+
}
284+
}
285+
}
286+
287+
private float distance(XZ xz1, XZ xz2) {
288+
return Mth.sqrt((xz2.x() - xz1.x()) * (xz2.x() - xz1.x()) + (xz2.z() - xz1.z()) * (xz2.z() - xz1.z()));
289+
}
290+
257291
@Override
258292
@SuppressWarnings("deprecation")
259293
public void addMouseOverText(TooltipList list) {
@@ -265,8 +299,10 @@ public void addMouseOverText(TooltipList list) {
265299

266300
Date date = new Date();
267301

302+
boolean altPressed = Screen.hasAltDown();
303+
268304
chunk.getClaimedDate().ifPresent(claimedDate -> {
269-
if (Screen.hasAltDown()) {
305+
if (altPressed) {
270306
list.add(Component.literal(claimedDate.toLocaleString()).withStyle(ChatFormatting.GRAY));
271307
} else {
272308
list.add(Component.literal(TimeUtils.prettyTimeString((date.getTime() - claimedDate.getTime()) / 1000L) + " ago").withStyle(ChatFormatting.GRAY));
@@ -276,26 +312,30 @@ public void addMouseOverText(TooltipList list) {
276312
chunk.getForceLoadedDate().ifPresent(forceLoadedDate -> {
277313
list.add(Component.translatable("ftbchunks.gui.force_loaded").withStyle(ChatFormatting.YELLOW));
278314

279-
String loadStr = Screen.hasAltDown() ?
315+
String loadStr = altPressed ?
280316
" " + forceLoadedDate.toLocaleString() :
281317
" " + TimeUtils.prettyTimeString((date.getTime() - forceLoadedDate.getTime()) / 1000L) + " ago";
282318
list.add(Component.literal(loadStr).withStyle(ChatFormatting.GRAY));
283319

284320
chunk.getForceLoadExpiryDate().ifPresent(expiryDate -> {
285321
list.add(Component.translatable("ftbchunks.gui.force_load_expires").withStyle(ChatFormatting.GOLD));
286-
String expireStr = Screen.hasAltDown() ?
322+
String expireStr = altPressed ?
287323
" " + expiryDate.toLocaleString() :
288324
" " + TimeUtils.prettyTimeString((expiryDate.getTime() - date.getTime()) / 1000L) + " from now";
289325
list.add(Component.literal(expireStr).withStyle(ChatFormatting.GRAY));
290326
});
291327

292-
if (!Screen.hasAltDown()) {
328+
if (!altPressed) {
293329
list.add(Component.translatable("ftbchunks.gui.hold_alt_for_dates").withStyle(ChatFormatting.DARK_GRAY));
294330
}
295331
if (team.getRankForPlayer(Minecraft.getInstance().player.getUUID()).isMemberOrBetter()){
296332
list.add(Component.translatable("ftbchunks.gui.mouse_wheel_expiry").withStyle(ChatFormatting.DARK_GRAY));
297333
}
298334
});
335+
336+
if (!altPressed && (chunk.getClaimedDate().isPresent() || chunk.getForceLoadedDate().isPresent())) {
337+
list.add(Component.translatable("ftbchunks.gui.hold_alt_for_dates").withStyle(ChatFormatting.DARK_GRAY));
338+
}
299339
});
300340
}
301341

common/src/main/resources/assets/ftbchunks/lang/en_us.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,8 @@
334334
"ftbchunks.command.bypass_protection_enabled": "Bypass Protection: Enabled",
335335
"ftbchunks.command.bypass_protection_disabled": "Bypass Protection: Disabled",
336336
"ftbchunks.command.view_loaded": "Chunks Loaded: %d. Check the map to see loaded chunks",
337-
"ftbchunks.command.waypoint_added": "Added waypoint '%s'"
337+
"ftbchunks.command.waypoint_added": "Added waypoint '%s'",
338+
"ftbchunks.claim_mode.freehand": "Freehand Claiming",
339+
"ftbchunks.claim_mode.rectangle": "Rectangular Claiming",
340+
"ftbchunks.claim_mode.circle": "Circular Claiming"
338341
}
5.91 KB
Loading
6.06 KB
Loading
5.83 KB
Loading

0 commit comments

Comments
 (0)