Skip to content

Commit 0dbe57a

Browse files
committed
more ink stuffs
1 parent a572f8d commit 0dbe57a

6 files changed

Lines changed: 60 additions & 58 deletions

File tree

src/main/java/de/dafuqs/spectrum/api/energy/color/InkColor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.dafuqs.spectrum.api.energy.color;
22

3-
import com.mojang.serialization.Codec;
4-
import com.mojang.serialization.DataResult;
3+
import com.mojang.serialization.*;
54
import de.dafuqs.spectrum.helpers.*;
65
import de.dafuqs.spectrum.registries.*;
76
import io.netty.buffer.*;

src/main/java/de/dafuqs/spectrum/api/energy/color/InkColors.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public class InkColors {
5757

5858
// in case an addon adds new colors
5959
// for places where we have to use a fixed size list, like GUIs with limited space
60-
public static final List<InkColor> BUILTIN_COLORS = List.of(InkColors.CYAN, InkColors.LIGHT_BLUE, InkColors.BLUE, InkColors.PURPLE, InkColors.MAGENTA, InkColors.PINK, InkColors.RED, InkColors.ORANGE, InkColors.YELLOW, InkColors.LIME, InkColors.GREEN, InkColors.BROWN, InkColors.BLACK, InkColors.GRAY, InkColors.LIGHT_GRAY, InkColors.WHITE);
60+
public static final List<InkColor> BUILTIN_COLORS = List.of(
61+
InkColors.CYAN, InkColors.LIGHT_BLUE, InkColors.BLUE, InkColors.PURPLE,
62+
InkColors.MAGENTA, InkColors.PINK, InkColors.RED, InkColors.ORANGE,
63+
InkColors.YELLOW, InkColors.LIME, InkColors.GREEN, InkColors.BROWN,
64+
InkColors.BLACK, InkColors.GRAY, InkColors.LIGHT_GRAY, InkColors.WHITE
65+
);
6166

6267
protected static InkColor register(String name, InkColor inkColor) {
6368
return Registry.register(SpectrumRegistries.INK_COLORS, SpectrumCommon.locate(name), inkColor);

src/main/java/de/dafuqs/spectrum/blocks/energy/ColorPickerBlockEntity.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,26 @@ public class ColorPickerBlockEntity extends LootableContainerBlockEntity impleme
4646
public static final int OUTPUT_SLOT_ID = 1;
4747
public static final long TICKS_PER_CONVERSION = 5;
4848
public static final long STORAGE_AMOUNT = 64 * 64 * 64 * 100;
49+
4950
public DefaultedList<ItemStack> inventory;
5051
protected TotalCappedInkStorage inkStorage;
5152
protected boolean paused;
5253
protected boolean inkDirty;
5354
protected @Nullable InkConvertingRecipe cachedRecipe;
54-
protected @Nullable InkColor selectedColor;
55+
protected Optional<InkColor> selectedColor = Optional.empty();
5556
private UUID ownerUUID;
56-
private final PropertyDelegate propertyDelegate = new BlockPosDelegate(pos) {
57+
private final PropertyDelegate propertyDelegate = new BlockPosDelegate(pos) { // TODO: sync using the ink sync packet instead? Currently it's hardcoded to InkColors that have a dyecolor representation
5758
@Override
5859
public int get(int index) {
5960
if (index == 3)
60-
return selectedColor == null ? -1 : selectedColor.getDyeColor().getId();
61+
return selectedColor.isEmpty() ? -1 : selectedColor.get().getDyeColor().getId();
6162
return super.get(index);
6263
}
6364

6465
@Override
6566
public void set(int index, int value) {
6667
if (index == 3)
67-
selectedColor = value == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(value));
68+
selectedColor = value == -1 ? Optional.empty() : Optional.of(InkColor.ofDyeColor(DyeColor.byId(value)));
6869
else
6970
super.set(index, value);
7071
}
@@ -80,7 +81,6 @@ public ColorPickerBlockEntity(BlockPos blockPos, BlockState blockState) {
8081

8182
this.inventory = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY);
8283
this.inkStorage = new TotalCappedInkStorage(STORAGE_AMOUNT, Map.of());
83-
this.selectedColor = null;
8484
}
8585

8686
@SuppressWarnings("unused")
@@ -119,7 +119,7 @@ public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLooku
119119
this.inkStorage = new TotalCappedInkStorage(storage.maxEnergyTotal(), storage.storedEnergy()));
120120
this.ownerUUID = PlayerOwned.readOwnerUUID(nbt);
121121
if (nbt.contains("SelectedColor", NbtElement.STRING_TYPE)) {
122-
this.selectedColor = InkColor.ofIdString(nbt.getString("SelectedColor")).orElse(InkColors.CYAN);
122+
this.selectedColor = InkColor.ofIdString(nbt.getString("SelectedColor"));
123123
}
124124
}
125125

@@ -131,8 +131,8 @@ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryL
131131
}
132132
CodecHelper.writeNbt(nbt, "InkStorage", InkStorageComponent.CODEC, new InkStorageComponent(this.inkStorage));
133133
PlayerOwned.writeOwnerUUID(nbt, this.ownerUUID);
134-
if (this.selectedColor != null) {
135-
nbt.putString("SelectedColor", this.selectedColor.getID().toString());
134+
if (this.selectedColor.isPresent()) {
135+
nbt.putString("SelectedColor", this.selectedColor.get().getID().toString());
136136
}
137137
}
138138

@@ -275,13 +275,13 @@ protected boolean tryFillInkContainer() {
275275
if (getOwnerIfOnline() instanceof ServerPlayerEntity serverPlayerEntity) {
276276
owner = serverPlayerEntity;
277277
}
278-
279-
if (this.selectedColor == null) {
278+
279+
if (this.selectedColor.isEmpty()) {
280280
for (InkColor color : InkColors.all()) {
281281
transferredAmount += tryTransferInk(owner, stack, itemStorage, color);
282282
}
283283
} else {
284-
transferredAmount = tryTransferInk(owner, stack, itemStorage, this.selectedColor);
284+
transferredAmount = tryTransferInk(owner, stack, itemStorage, this.selectedColor.get());
285285
}
286286

287287
if (transferredAmount > 0) {
@@ -299,14 +299,14 @@ private long tryTransferInk(ServerPlayerEntity owner, ItemStack stack, InkStorag
299299
}
300300
return amount;
301301
}
302-
303-
public void setSelectedColor(InkColor inkColor) {
302+
303+
public void setSelectedColor(Optional<InkColor> inkColor) {
304304
this.selectedColor = inkColor;
305305
this.paused = false;
306306
this.markDirty();
307307
}
308308

309-
public @Nullable InkColor getSelectedColor() {
309+
public Optional<InkColor> getSelectedColor() {
310310
return this.selectedColor;
311311
}
312312

src/main/java/de/dafuqs/spectrum/inventories/ColorPickerScreen.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import net.minecraft.client.gui.*;
1111
import net.minecraft.client.gui.screen.ingame.*;
1212
import net.minecraft.entity.player.*;
13+
import net.minecraft.registry.entry.*;
1314
import net.minecraft.text.*;
1415
import net.minecraft.util.*;
1516

17+
import java.util.*;
1618
import java.util.function.*;
1719

18-
public class ColorPickerScreen extends HandledScreen<ColorPickerScreenHandler> implements Consumer<InkColor> {
20+
public class ColorPickerScreen extends HandledScreen<ColorPickerScreenHandler> implements Consumer<Optional<RegistryEntry<InkColor>>> {
1921

2022
protected final Identifier BACKGROUND = SpectrumCommon.locate("textures/gui/container/color_picker.png");
2123
protected ColorSelectionWidget colorSelectionWidget;
@@ -64,7 +66,7 @@ protected void drawBackground(DrawContext drawContext, float delta, int mouseX,
6466

6567
this.inkGaugeWidget.draw(drawContext);
6668
this.inkMeterWidget.draw(drawContext);
67-
this.colorSelectionWidget.draw(drawContext);
69+
this.colorSelectionWidget.render(drawContext, mouseX, mouseY, delta);
6870

6971
// gauge blanket
7072
drawContext.drawTexture(BACKGROUND, startX + 52, startY + 18, 176, 0, 46, 46);
@@ -91,7 +93,7 @@ protected void drawMouseoverTooltip(DrawContext drawContext, int x, int y) {
9193
}
9294

9395
@Override
94-
public void accept(InkColor inkColor) {
96+
public void accept(Optional<RegistryEntry<InkColor>> inkColor) {
9597
ColorPickerBlockEntity colorPicker = this.handler.getBlockEntity();
9698
colorPicker.setSelectedColor(inkColor);
9799
ClientPlayNetworking.send(new InkColorSelectedC2SPayload(inkColor));

src/main/java/de/dafuqs/spectrum/inventories/ColorPickerScreenHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public ColorPickerScreenHandler(int syncId, PlayerInventory playerInventory, Pro
4747
this.player = playerInventory.player instanceof ServerPlayerEntity serverPlayerEntity ? serverPlayerEntity : null;
4848
this.world = playerInventory.player.getWorld();
4949
this.propertyDelegate = propertyDelegate;
50-
51-
var selectedColor = propertyDelegate.get(3) == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(propertyDelegate.get(3)));
50+
51+
InkColor selectedColor = propertyDelegate.get(3) == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(propertyDelegate.get(3)));
5252

5353
BlockEntity blockEntity = playerInventory.player.getWorld().getBlockEntity(getBlockPos());
5454
if (blockEntity instanceof ColorPickerBlockEntity colorPickerBlockEntity) {

src/main/java/de/dafuqs/spectrum/inventories/widgets/ColorSelectionWidget.java

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.client.gui.screen.*;
1111
import net.minecraft.client.gui.screen.narration.*;
1212
import net.minecraft.client.gui.widget.*;
13+
import net.minecraft.registry.entry.*;
1314
import net.minecraft.text.*;
1415
import net.minecraft.util.*;
1516
import net.minecraft.util.math.*;
@@ -26,7 +27,7 @@ public class ColorSelectionWidget extends ClickableWidget {
2627
protected final ColorPickerBlockEntity colorPicker;
2728

2829
@Nullable
29-
private Consumer<InkColor> changedListener;
30+
private Consumer<Optional<InkColor>> changedListener;
3031
protected final Screen screen;
3132

3233
final List<Pair<InkColor, Boolean>> usableColors = new ArrayList<>(); // stores if a certain color should be displayed
@@ -50,28 +51,48 @@ public ColorSelectionWidget(int x, int y, int selectedDotX, int selectedDotY, Sc
5051
}
5152
}
5253

53-
public void setChangedListener(@Nullable Consumer<InkColor> changedListener) {
54+
public void setChangedListener(@Nullable Consumer<Optional<InkColor>> changedListener) {
5455
this.changedListener = changedListener;
5556
}
5657

57-
private void onChanged(InkColor newColor) {
58+
private void onChanged(Optional<RegistryEntry<InkColor>> newColor) {
5859
if (this.changedListener != null) {
5960
this.changedListener.accept(newColor);
6061
}
6162
}
62-
63+
6364
@Override
64-
protected void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
65-
65+
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
66+
// draw selection icons
67+
int i = -1;
68+
int currentX = this.getX() + 1;
69+
int currentY = this.getY() + 1;
70+
for (Pair<InkColor, Boolean> color : usableColors) {
71+
if (color.getRight()) {
72+
fillQuad(context.getMatrices(), currentX, currentY, 5, 5, color.getLeft().value().getColorVec());
73+
}
74+
i = i + 1;
75+
currentX = currentX + 7;
76+
if (i == 7) {
77+
currentY = currentY + 7;
78+
currentX = this.getX() + 1;
79+
}
80+
}
81+
82+
// draw currently selected icon
83+
Optional<RegistryEntry<InkColor>> selectedColor = this.colorPicker.getSelectedColor();
84+
if (selectedColor.isPresent()) {
85+
fillQuad(context.getMatrices(), selectedDotX, selectedDotY, 4, 4, selectedColor.get().value().getColorVec());
86+
}
6687
}
67-
88+
6889
@Override
6990
public boolean mouseClicked(double mouseX, double mouseY, int button) {
7091
MinecraftClient client = MinecraftClient.getInstance();
7192

7293
if (isUnselection(mouseX, mouseY)) {
7394
client.player.playSound(SpectrumSoundEvents.BUTTON_CLICK, 1.0F, 1.0F);
74-
onChanged(null);
95+
onChanged(Optional.empty());
7596
}
7697

7798
boolean colorSelectionClicked = mouseX >= (double) this.getX() && mouseX < (double) (this.getX() + this.width) && mouseY >= (double) this.getY() && mouseY < (double) (this.getY() + this.height);
@@ -83,7 +104,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
83104
int verticalColorOffset = yOffset / 7;
84105
int newColorIndex = horizontalColorOffset + verticalColorOffset * 8;
85106

86-
Pair<InkColor, Boolean> clickedColor = usableColors.get(newColorIndex);
107+
Pair<RegistryEntry<InkColor>, Boolean> clickedColor = usableColors.get(newColorIndex);
87108
if (clickedColor.getRight()) {
88109
client.player.playSound(SpectrumSoundEvents.BUTTON_CLICK, 1.0F, 1.0F);
89110
onChanged(clickedColor.getLeft());
@@ -102,30 +123,6 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
102123
protected void appendClickableNarrations(NarrationMessageBuilder builder) {
103124
builder.put(NarrationPart.TITLE, Text.translatable("spectrum.narration.color_selection", this.colorPicker.getSelectedColor()));
104125
}
105-
106-
public void draw(DrawContext drawContext) {
107-
// draw selection icons
108-
int i = -1;
109-
int currentX = this.getX() + 1;
110-
int currentY = this.getY() + 1;
111-
for (Pair<InkColor, Boolean> color : usableColors) {
112-
if (color.getRight()) {
113-
fillQuad(drawContext.getMatrices(), currentX, currentY, 5, 5, color.getLeft().getColorVec());
114-
}
115-
i = i + 1;
116-
currentX = currentX + 7;
117-
if (i == 7) {
118-
currentY = currentY + 7;
119-
currentX = this.getX() + 1;
120-
}
121-
}
122-
123-
// draw currently selected icon
124-
InkColor selectedColor = this.colorPicker.getSelectedColor();
125-
if (selectedColor != null) {
126-
fillQuad(drawContext.getMatrices(), selectedDotX, selectedDotY, 4, 4, selectedColor.getColorVec());
127-
}
128-
}
129126

130127
private boolean isUnselection(double mouseX, double mouseY) {
131128
return mouseX >= (double) selectedDotX && mouseX < (double) (selectedDotX + 4) && mouseY >= (double) selectedDotY && mouseY < (double) (selectedDotY + 4);
@@ -141,17 +138,16 @@ public void drawMouseoverTooltip(DrawContext drawContext, int mouseX, int mouseY
141138
if (overUnselection) {
142139
drawContext.drawTooltip(client.textRenderer, List.of(Text.translatable("spectrum.tooltip.ink_powered.unselect_color")), Optional.empty(), getX(), getY());
143140
} else {
144-
145141
int xOffset = MathHelper.floor(mouseX) - this.getX();
146142
int yOffset = MathHelper.floor(mouseY) - this.getY();
147143

148144
int horizontalColorOffset = xOffset / 7;
149145
int verticalColorOffset = yOffset / 7;
150146
int newColorIndex = horizontalColorOffset + verticalColorOffset * 8;
151147

152-
Pair<InkColor, Boolean> hoveredColor = usableColors.get(newColorIndex);
148+
Pair<RegistryEntry<InkColor>, Boolean> hoveredColor = usableColors.get(newColorIndex);
153149
if (hoveredColor.getRight()) {
154-
drawContext.drawTooltip(client.textRenderer, List.of(hoveredColor.getLeft().getName()), Optional.empty(), getX(), getY());
150+
drawContext.drawTooltip(client.textRenderer, List.of(hoveredColor.getLeft().value().getName()), Optional.empty(), getX(), getY());
155151
} else {
156152
drawContext.drawTooltip(client.textRenderer, List.of(Text.translatable("spectrum.tooltip.ink_powered.unselect_color")), Optional.empty(), getX(), getY());
157153
}

0 commit comments

Comments
 (0)