summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/funnyteleporters/ColouredChestConfigGUI.java
blob: 6a2ee408aa3327adc7ff8e57426c00e8cc95d2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package moe.nea.funnyteleporters;

import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElementInterface;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Formatting;
import net.minecraft.util.Unit;
import net.minecraft.util.math.MathHelper;

import javax.xml.crypto.Data;
import java.text.Format;
import java.util.ArrayList;
import java.util.Collections;

public class ColouredChestConfigGUI extends SimpleGui {

	private final ColouredChestBlockEntity blockEntity;

	public ColouredChestConfigGUI(ColouredChestBlockEntity blockEntity, ServerPlayerEntity player) {
		super(ScreenHandlerType.GENERIC_9X3, player, false);
		setTitle(Text.literal("Funny Enderchest"));
		this.blockEntity = blockEntity;
		this.updateFromBlockEntity();
	}

	ItemStack createUpItem(int frequency, String direction) {
		var is = new ItemStack(Items.ARROW);
		is.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Frequency " + frequency + " " + direction).setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.AQUA)));
		return is;
	}

	private ItemStack createFrequencyItem(int frequency, DyeColor dyeColor) {
		var is = new ItemStack(DyeItem.byColor(dyeColor));
		is.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Frequency " + frequency + ": " + dyeColor.getId()).setStyle(Style.EMPTY.withItalic(false).withColor(dyeColor.getSignColor())));
		return is;
	}

	@Override
	public boolean onClick(int index, ClickType type, SlotActionType action, GuiElementInterface element) {
		int x = index % width;
		int y = index / width;
		if (x == 0 || x == 1 || x == 2) {
			cycleColour(x, y == 0 ? -1 : y == 2 ? 1 : 0);
		}
		if (index == width + 5) {
			new ColouredChestViewScreen(player, blockEntity).open();
		}
		return super.onClick(index, type, action, element);
	}

	void cycleColour(int frequencyIndex, int direction) {
		var newList = new ArrayList<>(blockEntity.extra.frequency());
		newList.set(frequencyIndex, cycleColour(newList.get(frequencyIndex), direction));
		blockEntity.extra = new ColouredChestBlockEntity.ExtraData(Collections.unmodifiableList(newList));
		updateFromBlockEntity();
		blockEntity.markDirty();
	}

	private DyeColor cycleColour(DyeColor dyeColor, int direction) {
		return DyeColor.values()[MathHelper.floorMod(dyeColor.ordinal() + direction, DyeColor.values().length)];
	}

	public void updateFromBlockEntity() {
		for (int i = 0; i < 3; i++) {
			setSlot(i, createUpItem(i, "up"));
			setSlot(i + width, createFrequencyItem(i, blockEntity.extra.frequency().get(i)));
			setSlot(i + 2 * width, createUpItem(i, "down"));
		}
		for (int x = 3; x < 9; x++) {
			for (int y = 0; y < 3; y++) {
				var s = new ItemStack(Items.BLACK_STAINED_GLASS_PANE);
				s.set(DataComponentTypes.CUSTOM_NAME, Text.empty());
				s.set(DataComponentTypes.HIDE_TOOLTIP, Unit.INSTANCE);
				setSlot(y * width + x, s);
			}
		}
		{
			var s = new ItemStack(Items.CHEST);
			s.set(DataComponentTypes.CUSTOM_NAME, Text.literal("View chest").setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.DARK_RED)));
			setSlot(width + 5, s);
		}
	}
}