Skip to content

Commit

Permalink
more ink stuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Jan 19, 2025
1 parent a572f8d commit 0dbe57a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.dafuqs.spectrum.api.energy.color;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.registries.*;
import io.netty.buffer.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public class InkColors {

// in case an addon adds new colors
// for places where we have to use a fixed size list, like GUIs with limited space
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);
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
);

protected static InkColor register(String name, InkColor inkColor) {
return Registry.register(SpectrumRegistries.INK_COLORS, SpectrumCommon.locate(name), inkColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,26 @@ public class ColorPickerBlockEntity extends LootableContainerBlockEntity impleme
public static final int OUTPUT_SLOT_ID = 1;
public static final long TICKS_PER_CONVERSION = 5;
public static final long STORAGE_AMOUNT = 64 * 64 * 64 * 100;

public DefaultedList<ItemStack> inventory;
protected TotalCappedInkStorage inkStorage;
protected boolean paused;
protected boolean inkDirty;
protected @Nullable InkConvertingRecipe cachedRecipe;
protected @Nullable InkColor selectedColor;
protected Optional<InkColor> selectedColor = Optional.empty();
private UUID ownerUUID;
private final PropertyDelegate propertyDelegate = new BlockPosDelegate(pos) {
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
@Override
public int get(int index) {
if (index == 3)
return selectedColor == null ? -1 : selectedColor.getDyeColor().getId();
return selectedColor.isEmpty() ? -1 : selectedColor.get().getDyeColor().getId();
return super.get(index);
}

@Override
public void set(int index, int value) {
if (index == 3)
selectedColor = value == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(value));
selectedColor = value == -1 ? Optional.empty() : Optional.of(InkColor.ofDyeColor(DyeColor.byId(value)));
else
super.set(index, value);
}
Expand All @@ -80,7 +81,6 @@ public ColorPickerBlockEntity(BlockPos blockPos, BlockState blockState) {

this.inventory = DefaultedList.ofSize(INVENTORY_SIZE, ItemStack.EMPTY);
this.inkStorage = new TotalCappedInkStorage(STORAGE_AMOUNT, Map.of());
this.selectedColor = null;
}

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

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

Expand Down Expand Up @@ -275,13 +275,13 @@ protected boolean tryFillInkContainer() {
if (getOwnerIfOnline() instanceof ServerPlayerEntity serverPlayerEntity) {
owner = serverPlayerEntity;
}

if (this.selectedColor == null) {
if (this.selectedColor.isEmpty()) {
for (InkColor color : InkColors.all()) {
transferredAmount += tryTransferInk(owner, stack, itemStorage, color);
}
} else {
transferredAmount = tryTransferInk(owner, stack, itemStorage, this.selectedColor);
transferredAmount = tryTransferInk(owner, stack, itemStorage, this.selectedColor.get());
}

if (transferredAmount > 0) {
Expand All @@ -299,14 +299,14 @@ private long tryTransferInk(ServerPlayerEntity owner, ItemStack stack, InkStorag
}
return amount;
}

public void setSelectedColor(InkColor inkColor) {
public void setSelectedColor(Optional<InkColor> inkColor) {
this.selectedColor = inkColor;
this.paused = false;
this.markDirty();
}

public @Nullable InkColor getSelectedColor() {
public Optional<InkColor> getSelectedColor() {
return this.selectedColor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import net.minecraft.client.gui.*;
import net.minecraft.client.gui.screen.ingame.*;
import net.minecraft.entity.player.*;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import net.minecraft.util.*;

import java.util.*;
import java.util.function.*;

public class ColorPickerScreen extends HandledScreen<ColorPickerScreenHandler> implements Consumer<InkColor> {
public class ColorPickerScreen extends HandledScreen<ColorPickerScreenHandler> implements Consumer<Optional<RegistryEntry<InkColor>>> {

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

this.inkGaugeWidget.draw(drawContext);
this.inkMeterWidget.draw(drawContext);
this.colorSelectionWidget.draw(drawContext);
this.colorSelectionWidget.render(drawContext, mouseX, mouseY, delta);

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

@Override
public void accept(InkColor inkColor) {
public void accept(Optional<RegistryEntry<InkColor>> inkColor) {
ColorPickerBlockEntity colorPicker = this.handler.getBlockEntity();
colorPicker.setSelectedColor(inkColor);
ClientPlayNetworking.send(new InkColorSelectedC2SPayload(inkColor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public ColorPickerScreenHandler(int syncId, PlayerInventory playerInventory, Pro
this.player = playerInventory.player instanceof ServerPlayerEntity serverPlayerEntity ? serverPlayerEntity : null;
this.world = playerInventory.player.getWorld();
this.propertyDelegate = propertyDelegate;

var selectedColor = propertyDelegate.get(3) == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(propertyDelegate.get(3)));
InkColor selectedColor = propertyDelegate.get(3) == -1 ? null : InkColor.ofDyeColor(DyeColor.byId(propertyDelegate.get(3)));

BlockEntity blockEntity = playerInventory.player.getWorld().getBlockEntity(getBlockPos());
if (blockEntity instanceof ColorPickerBlockEntity colorPickerBlockEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.gui.screen.*;
import net.minecraft.client.gui.screen.narration.*;
import net.minecraft.client.gui.widget.*;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
import net.minecraft.util.math.*;
Expand All @@ -26,7 +27,7 @@ public class ColorSelectionWidget extends ClickableWidget {
protected final ColorPickerBlockEntity colorPicker;

@Nullable
private Consumer<InkColor> changedListener;
private Consumer<Optional<InkColor>> changedListener;
protected final Screen screen;

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

public void setChangedListener(@Nullable Consumer<InkColor> changedListener) {
public void setChangedListener(@Nullable Consumer<Optional<InkColor>> changedListener) {
this.changedListener = changedListener;
}

private void onChanged(InkColor newColor) {
private void onChanged(Optional<RegistryEntry<InkColor>> newColor) {
if (this.changedListener != null) {
this.changedListener.accept(newColor);
}
}

@Override
protected void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {

protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
// draw selection icons
int i = -1;
int currentX = this.getX() + 1;
int currentY = this.getY() + 1;
for (Pair<InkColor, Boolean> color : usableColors) {
if (color.getRight()) {
fillQuad(context.getMatrices(), currentX, currentY, 5, 5, color.getLeft().value().getColorVec());
}
i = i + 1;
currentX = currentX + 7;
if (i == 7) {
currentY = currentY + 7;
currentX = this.getX() + 1;
}
}

// draw currently selected icon
Optional<RegistryEntry<InkColor>> selectedColor = this.colorPicker.getSelectedColor();
if (selectedColor.isPresent()) {
fillQuad(context.getMatrices(), selectedDotX, selectedDotY, 4, 4, selectedColor.get().value().getColorVec());
}
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
MinecraftClient client = MinecraftClient.getInstance();

if (isUnselection(mouseX, mouseY)) {
client.player.playSound(SpectrumSoundEvents.BUTTON_CLICK, 1.0F, 1.0F);
onChanged(null);
onChanged(Optional.empty());
}

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

Pair<InkColor, Boolean> clickedColor = usableColors.get(newColorIndex);
Pair<RegistryEntry<InkColor>, Boolean> clickedColor = usableColors.get(newColorIndex);
if (clickedColor.getRight()) {
client.player.playSound(SpectrumSoundEvents.BUTTON_CLICK, 1.0F, 1.0F);
onChanged(clickedColor.getLeft());
Expand All @@ -102,30 +123,6 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
protected void appendClickableNarrations(NarrationMessageBuilder builder) {
builder.put(NarrationPart.TITLE, Text.translatable("spectrum.narration.color_selection", this.colorPicker.getSelectedColor()));
}

public void draw(DrawContext drawContext) {
// draw selection icons
int i = -1;
int currentX = this.getX() + 1;
int currentY = this.getY() + 1;
for (Pair<InkColor, Boolean> color : usableColors) {
if (color.getRight()) {
fillQuad(drawContext.getMatrices(), currentX, currentY, 5, 5, color.getLeft().getColorVec());
}
i = i + 1;
currentX = currentX + 7;
if (i == 7) {
currentY = currentY + 7;
currentX = this.getX() + 1;
}
}

// draw currently selected icon
InkColor selectedColor = this.colorPicker.getSelectedColor();
if (selectedColor != null) {
fillQuad(drawContext.getMatrices(), selectedDotX, selectedDotY, 4, 4, selectedColor.getColorVec());
}
}

private boolean isUnselection(double mouseX, double mouseY) {
return mouseX >= (double) selectedDotX && mouseX < (double) (selectedDotX + 4) && mouseY >= (double) selectedDotY && mouseY < (double) (selectedDotY + 4);
Expand All @@ -141,17 +138,16 @@ public void drawMouseoverTooltip(DrawContext drawContext, int mouseX, int mouseY
if (overUnselection) {
drawContext.drawTooltip(client.textRenderer, List.of(Text.translatable("spectrum.tooltip.ink_powered.unselect_color")), Optional.empty(), getX(), getY());
} else {

int xOffset = MathHelper.floor(mouseX) - this.getX();
int yOffset = MathHelper.floor(mouseY) - this.getY();

int horizontalColorOffset = xOffset / 7;
int verticalColorOffset = yOffset / 7;
int newColorIndex = horizontalColorOffset + verticalColorOffset * 8;

Pair<InkColor, Boolean> hoveredColor = usableColors.get(newColorIndex);
Pair<RegistryEntry<InkColor>, Boolean> hoveredColor = usableColors.get(newColorIndex);
if (hoveredColor.getRight()) {
drawContext.drawTooltip(client.textRenderer, List.of(hoveredColor.getLeft().getName()), Optional.empty(), getX(), getY());
drawContext.drawTooltip(client.textRenderer, List.of(hoveredColor.getLeft().value().getName()), Optional.empty(), getX(), getY());
} else {
drawContext.drawTooltip(client.textRenderer, List.of(Text.translatable("spectrum.tooltip.ink_powered.unselect_color")), Optional.empty(), getX(), getY());
}
Expand Down

0 comments on commit 0dbe57a

Please sign in to comment.