Skip to content

Commit e065dbb

Browse files
Merge pull request #1 from LitleProgrammer/dev/base-functions
Dev/base functions
2 parents 41467b4 + 975d1c0 commit e065dbb

File tree

15 files changed

+861
-20
lines changed

15 files changed

+861
-20
lines changed

build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repositories {
1818
}
1919

2020
dependencies {
21-
compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT"
21+
compileOnly "org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT"
2222
}
2323

2424
def targetJavaVersion = 17
@@ -31,6 +31,10 @@ java {
3131
}
3232
}
3333

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

@@ -46,4 +50,4 @@ processResources {
4650
filesMatching('plugin.yml') {
4751
expand props
4852
}
49-
}
53+
}

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

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package de.littleprogrammer.guiapi;
2+
3+
import de.littleprogrammer.guiapi.enums.ServerVersion;
4+
import de.littleprogrammer.guiapi.listeners.GuiEvents;
5+
import de.littleprogrammer.guiapi.listeners.MoveListener;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
import org.bukkit.scheduler.BukkitScheduler;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.UUID;
15+
16+
public final class GuiApi {
17+
18+
private JavaPlugin plugin;
19+
private static GuiApi instance;
20+
private ServerVersion version;
21+
private final Listener listener = new GuiEvents();
22+
private final Listener moveListener = new MoveListener();
23+
private Map<UUID, SimpleGui> guis = new HashMap<>();
24+
25+
public GuiApi(JavaPlugin plugin) {
26+
this.plugin = plugin;
27+
instance = this;
28+
}
29+
30+
/*@Override
31+
public void onEnable() {
32+
// Plugin startup logic
33+
this.plugin = this;
34+
instance = this;
35+
36+
init();
37+
}
38+
39+
@Override
40+
public void onDisable() {
41+
// Plugin shutdown logic
42+
}*/
43+
44+
public void init() {
45+
//This method checks the server version, to determine weather it should use the new 1.20.2 teleport interpolation or my own teleport interpolation
46+
String secIndicator = plugin.getServer().getBukkitVersion().split("\\.")[1];
47+
String preTrdIndicator = plugin.getServer().getBukkitVersion().split("\\.")[2];
48+
String trdIndicator = preTrdIndicator.split("-")[0];
49+
50+
if (Integer.parseInt(secIndicator) == 20) {
51+
if (Integer.parseInt(trdIndicator) >= 2) {
52+
version = ServerVersion.POST_1_20_2;
53+
} else {
54+
version = ServerVersion.PRE_1_20_2;
55+
}
56+
} else {
57+
if (Integer.parseInt(secIndicator) > 20) {
58+
version = ServerVersion.POST_1_20_2;
59+
} else {
60+
version = ServerVersion.PRE_1_20_2;
61+
}
62+
}
63+
64+
//register the two listeners needed
65+
getPlugin().getServer().getPluginManager().registerEvents(this.listener, this.plugin);
66+
getPlugin().getServer().getPluginManager().registerEvents(this.moveListener, this.plugin);
67+
}
68+
69+
public JavaPlugin getPlugin() {return this.plugin;}
70+
public static GuiApi getInstance() {return instance;}
71+
public ServerVersion getVersion() {
72+
return version;
73+
}
74+
public static BukkitScheduler getScheduler() {
75+
return GuiApi.getInstance().getPlugin().getServer().getScheduler();
76+
}
77+
78+
public SimpleGui getGUI(UUID uuid) {
79+
return guis.get(uuid);
80+
}
81+
82+
public SimpleGui getGUI(Player player) {
83+
return guis.get(player.getUniqueId());
84+
}
85+
86+
public Map<UUID, SimpleGui> getGuis() {
87+
return guis;
88+
}
89+
90+
public Listener getListener() {
91+
return listener;
92+
}
93+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.*;
13+
14+
public class SimpleGui {
15+
16+
private Player player;
17+
private final UUID uuid;
18+
private final Map<UUID, Component> components;
19+
private final Map<UUID, Button> buttons;
20+
private final String title;
21+
private Component content;
22+
private Location centerLocation;
23+
private boolean open;
24+
25+
public SimpleGui(String title) {
26+
this.title = title;
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+
centerLocation = new Location(player.getWorld(), 0, 0, 0);
73+
74+
for (Component component : components.values()) {
75+
component.spawn();
76+
component.show(player);
77+
}
78+
open = true;
79+
return this;
80+
}
81+
82+
public Component getComponent(UUID uuid) {
83+
return components.get(uuid);
84+
}
85+
86+
public Location getCenterLocation() {
87+
return centerLocation;
88+
}
89+
90+
public Player getPlayer() {
91+
return player;
92+
}
93+
94+
public List<Component> getComponents() {
95+
return new ArrayList<>(components.values());
96+
}
97+
98+
public int getButtonAmount() {
99+
return buttons.size();
100+
}
101+
102+
public boolean isOpen() {
103+
return open;
104+
}
105+
106+
public SimpleGui addButton(Button button) {
107+
if (buttons.size() < 3) {
108+
components.put(button.getUniqueId(), button);
109+
buttons.put(button.getUniqueId(), button);
110+
button.setGui(this);
111+
}
112+
return this;
113+
}
114+
115+
public SimpleGui addContent(Text content) {
116+
if (this.content != null) {
117+
components.remove(this.content.getUniqueId());
118+
this.content.remove();
119+
}
120+
this.content = content;
121+
components.put(content.getUniqueId(), content);
122+
content.setGui(this);
123+
return this;
124+
}
125+
}

0 commit comments

Comments
 (0)