Skip to content

Commit 008b4c8

Browse files
Done so much
- Added method for opening, closing gui - Added support for multiple buttons - Got stuck on a math problem where I have to calculate the position of 2 buttons in 3d space depending on the players position
1 parent b75979b commit 008b4c8

File tree

13 files changed

+283
-125
lines changed

13 files changed

+283
-125
lines changed

build.gradle

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,10 @@ java {
3131
}
3232
}
3333

34-
jar {
35-
destinationDirectory.set(file("/Users/linusglimm/Desktop/mineopoly stuff/servers/spigotTest/plugins"))
36-
}
37-
3834
tasks.withType(JavaCompile).configureEach {
3935
options.encoding = 'UTF-8'
4036

4137
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
4238
options.release.set(targetJavaVersion)
4339
}
4440
}
45-
46-
processResources {
47-
def props = [version: version]
48-
inputs.properties props
49-
filteringCharset 'UTF-8'
50-
filesMatching('plugin.yml') {
51-
expand props
52-
}
53-
}

src/main/java/de/littleprogrammer/guiapi/GUI.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/main/java/de/littleprogrammer/guiapi/Api.java renamed to src/main/java/de/littleprogrammer/guiapi/GuiApi.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import de.littleprogrammer.guiapi.enums.ServerVersion;
44
import de.littleprogrammer.guiapi.listeners.GuiEvents;
5+
import de.littleprogrammer.guiapi.listeners.MoveListener;
56
import org.bukkit.entity.Player;
67
import org.bukkit.event.Listener;
78
import org.bukkit.plugin.java.JavaPlugin;
@@ -11,15 +12,15 @@
1112
import java.util.Map;
1213
import java.util.UUID;
1314

14-
public final class Api {
15+
public final class GuiApi {
1516

1617
private final JavaPlugin plugin;
17-
private static Api instance;
18+
private static GuiApi instance;
1819
private ServerVersion version;
1920
private final Listener listener = new GuiEvents();
20-
private Map<UUID, GUI> guis = new HashMap<>();
21+
private Map<UUID, SimpleGui> guis = new HashMap<>();
2122

22-
private Api(JavaPlugin plugin) {
23+
public GuiApi(JavaPlugin plugin) {
2324
this.plugin = plugin;
2425
instance = this;
2526
}
@@ -44,26 +45,27 @@ public void init() {
4445
}
4546

4647
this.plugin.getServer().getPluginManager().registerEvents(this.listener, this.plugin);
48+
this.plugin.getServer().getPluginManager().registerEvents(new MoveListener(), this.plugin);
4749
}
4850

4951
public JavaPlugin getPlugin() {return this.plugin;}
50-
public static Api getInstance() {return instance;}
52+
public static GuiApi getInstance() {return instance;}
5153
public ServerVersion getVersion() {
5254
return version;
5355
}
5456
public static BukkitScheduler getScheduler() {
55-
return Api.getInstance().getPlugin().getServer().getScheduler();
57+
return GuiApi.getInstance().getPlugin().getServer().getScheduler();
5658
}
5759

58-
public GUI getGUI(UUID uuid) {
60+
public SimpleGui getGUI(UUID uuid) {
5961
return guis.get(uuid);
6062
}
6163

62-
public GUI getGUI(Player player) {
64+
public SimpleGui getGUI(Player player) {
6365
return guis.get(player.getUniqueId());
6466
}
6567

66-
public Map<UUID, GUI> getGuis() {
68+
public Map<UUID, SimpleGui> getGuis() {
6769
return guis;
6870
}
6971

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package de.littleprogrammer.guiapi;
2+
3+
import de.littleprogrammer.guiapi.components.Button;
4+
import de.littleprogrammer.guiapi.components.Component;
5+
import de.littleprogrammer.guiapi.components.Text;
6+
import de.littleprogrammer.guiapi.enums.ServerVersion;
7+
import de.littleprogrammer.guiapi.utils.Calculations;
8+
import de.littleprogrammer.guiapi.utils.TeleportInterpolator;
9+
import org.bukkit.Location;
10+
import org.bukkit.entity.Player;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.UUID;
15+
16+
public class SimpleGui {
17+
18+
private Player player;
19+
private final UUID uuid;
20+
private final Map<UUID, Component> components;
21+
private final Map<UUID, Button> buttons;
22+
private Component content;
23+
private Location centerLocation;
24+
private boolean open;
25+
26+
public SimpleGui(String title, int gridWidth, int gridHeight) {
27+
this.uuid = UUID.randomUUID();
28+
this.components = new HashMap<>();
29+
this.buttons = new HashMap<>();
30+
}
31+
32+
public void updatePosition(Location playerLoc) {
33+
if (player != null) {
34+
centerLocation = Calculations.calculateInventoryCenter(playerLoc);
35+
36+
if (GuiApi.getInstance().getVersion().equals(ServerVersion.PRE_1_20_2)) {
37+
for (Component component : components.values()) {
38+
Location newComponentLocation = Calculations.calculateComponentLocation(this, component, buttons.size());
39+
40+
TeleportInterpolator teleportInterpolator = new TeleportInterpolator(component.getEntity(), newComponentLocation, 5, 1);
41+
teleportInterpolator.startInterpolation();
42+
}
43+
} else {
44+
for (Component component : components.values()) {
45+
Location newComponentLocation = Calculations.calculateComponentLocation(this, component, buttons.size());
46+
47+
component.getDisplay().setTeleportDuration(5);
48+
component.getDisplay().teleport(newComponentLocation);
49+
}
50+
}
51+
}
52+
}
53+
54+
public void close() {
55+
//close GUI
56+
GuiApi.getInstance().getGuis().remove(player.getUniqueId());
57+
58+
for (Component component : components.values()) {
59+
component.hide(player);
60+
component.remove();
61+
}
62+
open = false;
63+
}
64+
65+
public SimpleGui open(Player player) {
66+
if (this.player != null && this.player.getUniqueId().equals(player.getUniqueId())) {
67+
//close GUI and open for the new player
68+
close();
69+
}
70+
this.player = player;
71+
GuiApi.getInstance().getGuis().put(player.getUniqueId(), this);
72+
73+
for (Component component : components.values()) {
74+
component.spawn();
75+
component.show(player);
76+
}
77+
open = true;
78+
return this;
79+
}
80+
81+
public Component getComponent(UUID uuid) {
82+
return components.get(uuid);
83+
}
84+
85+
public Location getCenterLocation() {
86+
return centerLocation;
87+
}
88+
89+
public boolean isOpen() {
90+
return open;
91+
}
92+
93+
public SimpleGui addButton(Button button) {
94+
if (buttons.size() < 3) {
95+
components.put(button.getUniqueId(), button);
96+
buttons.put(button.getUniqueId(), button);
97+
}
98+
return this;
99+
}
100+
101+
public SimpleGui addContent(Text content) {
102+
if (content != null) {
103+
content.remove();
104+
}
105+
this.content = content;
106+
return this;
107+
}
108+
109+
public Player getPlayer() {
110+
return player;
111+
}
112+
}

src/main/java/de/littleprogrammer/guiapi/commands/SpawnButtonCommand.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/de/littleprogrammer/guiapi/components/Button.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package de.littleprogrammer.guiapi.components;
22

3-
import de.littleprogrammer.guiapi.Api;
4-
import de.littleprogrammer.guiapi.GUI;
3+
import de.littleprogrammer.guiapi.GuiApi;
4+
import de.littleprogrammer.guiapi.SimpleGui;
55
import de.littleprogrammer.guiapi.utils.Calculations;
6-
import org.bukkit.Bukkit;
76
import org.bukkit.Location;
87
import org.bukkit.entity.*;
98
import org.bukkit.event.player.PlayerInteractEntityEvent;
@@ -21,25 +20,39 @@ public class Button implements Component {
2120
private Location location;
2221
private Player player;
2322
private Consumer<PlayerInteractEntityEvent> clickAction;
24-
private GUI gui;
23+
private int slot;
2524

26-
public Button(GUI gui, Player player, String texture, String localizedName) {
25+
public Button(Player player, String texture, String localizedName, int slot) {
2726
this.player = player;
2827
this.texture = texture;
2928
this.localizedName = localizedName;
30-
this.gui = gui;
31-
uuid = UUID.randomUUID();
29+
this.slot = slot;
3230

33-
spawn();
31+
uuid = UUID.randomUUID();
3432
}
3533

36-
private void spawn() {
37-
textDisplay = (TextDisplay) player.getWorld().spawnEntity(Calculations.calculateInventoryLoc(player.getLocation()), EntityType.TEXT_DISPLAY);
34+
public void spawn() {
35+
textDisplay = (TextDisplay) player.getWorld().spawnEntity(Calculations.calculateInventoryCenter(player.getLocation()), EntityType.TEXT_DISPLAY);
36+
textDisplay.setCustomName(uuid.toString());
37+
textDisplay.setCustomNameVisible(false);
3838
textDisplay.setText(texture);
3939
textDisplay.setGlowing(true);
4040
textDisplay.setBillboard(Display.Billboard.CENTER);
4141
textDisplay.setDisplayWidth(30);
4242
textDisplay.setDisplayHeight(30);
43+
textDisplay.setVisibleByDefault(false);
44+
}
45+
46+
public void show(Player player) {
47+
player.showEntity(GuiApi.getInstance().getPlugin(), textDisplay);
48+
}
49+
50+
public void hide(Player player) {
51+
player.hideEntity(GuiApi.getInstance().getPlugin(), textDisplay);
52+
}
53+
54+
public void remove() {
55+
textDisplay.remove();
4356
}
4457

4558
@Nonnull
@@ -60,4 +73,12 @@ public Entity getEntity() {
6073
public Display getDisplay() {
6174
return textDisplay;
6275
}
76+
77+
public UUID getUniqueId() {
78+
return uuid;
79+
}
80+
81+
public int getSlot() {
82+
return slot;
83+
}
6384
}

src/main/java/de/littleprogrammer/guiapi/components/Component.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
import org.bukkit.entity.Display;
44
import org.bukkit.entity.Entity;
5+
import org.bukkit.entity.Player;
6+
7+
import java.util.UUID;
58

69
public interface Component {
710

811
public Entity getEntity();
912
public Display getDisplay();
13+
public UUID getUniqueId();
14+
public void show(Player player);
15+
public void hide(Player player);
16+
public void remove();
17+
public void spawn();
1018

1119
}

0 commit comments

Comments
 (0)