summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/funnyteleporters/TeleporterNexusEditorScreen.java
blob: 7fe55a6382cccc94356d959677f03128399f6fd1 (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
package moe.nea.funnyteleporters;

import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public class TeleporterNexusEditorScreen extends SimpleGui {
	final TeleporterNexusBlockEntity blockEntity;
	final TeleporterDestination dest;

	public TeleporterNexusEditorScreen(TeleporterNexusBlockEntity blockEntity, TeleporterDestination dest, ServerPlayerEntity player) {
		super(ScreenHandlerType.GENERIC_9X3, player, false);
		this.blockEntity = blockEntity;
		this.dest = dest;
		setSlots();
	}

	void setSlots() {
		setTitle(Text.literal("Set Icon for " + blockEntity.getName(dest).orElse("teleporter")));
		for (int i = 0; i < width * height; i++) {
			setSlot(i, Utils.createBlankBlack());
		}
		setSlot(4 + 9,
		        GuiElementBuilder.from(new ItemStack(blockEntity.getIcon(dest)))
		                         .setName(Text.literal("Click item in your inventory to set icon.")));
		setSlot(2 + 9,
		        GuiElementBuilder.from(new ItemStack(Items.OAK_SIGN))
		                         .setName(Text.literal("Click here to change name"))
		                         .addLoreLine(Text.literal("Click here to set a name for the teleport destination")
		                                          .setStyle(Utils.colouredLoreStyle(Formatting.AQUA)))
		                         .setCallback(this::handleNameChange));
		setSlot(9 + 6,
		        GuiElementBuilder.from(new ItemStack(Items.BARRIER))
		                         .setName(Text.literal("Delete").withColor(0xFFFF0000))
		                         .addLoreLine(Text.literal("Shift-Click here to remove this location")
		                                          .setStyle(Utils.colouredLoreStyle(Formatting.RED)))
		                         .addLoreLine(Text.literal("from the teleport nexus")
		                                          .setStyle(Utils.colouredLoreStyle(Formatting.RED)))
		                         .setCallback(this::handleDelete));
	}

	void handleNameChange() {
		new TeleporterNexusNameEditorScreen(this).open();
	}

	void handleDelete(ClickType clickType) {
		if (clickType == ClickType.MOUSE_LEFT_SHIFT) {
			blockEntity.removeDestination(dest);
			back();
		} else {
			player.sendMessage(Text.literal("You need to shift click to delete locations."));
		}
	}

	@Override
	public void onClose() {
		back();
	}

	void back() {
		new TeleporterNexusScreen(blockEntity, player).open();
	}

	@Override
	public boolean onAnyClick(int index, ClickType type, SlotActionType action) {
		var slot = getSlotRedirectOrPlayer(index);
		if (slot == null) return true;
		var stack = slot.getStack();
		if (!stack.isEmpty()) {
			blockEntity.setIcon(dest, stack.getItem());
			setSlots();
			if (screenHandler != null)
				screenHandler.syncState();
		}
		return false;
	}
}