Skip to content

Commit 9b0ea16

Browse files
Done some stuff
- Added a button component - Made button fixed in front of the player - Made difference between 1.20.1 and 1.20.2 in the method used to move the display
1 parent f403529 commit 9b0ea16

File tree

11 files changed

+236
-1
lines changed

11 files changed

+236
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
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
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
11
package de.littleprogrammer.guiapi;
22

3+
import de.littleprogrammer.guiapi.commands.SpawnButtonCommand;
4+
import de.littleprogrammer.guiapi.components.Button;
5+
import de.littleprogrammer.guiapi.enums.ServerVersion;
6+
import de.littleprogrammer.guiapi.listeners.MoveListener;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.Server;
39
import org.bukkit.plugin.java.JavaPlugin;
410

11+
import java.util.HashMap;
12+
import java.util.Map;
13+
import java.util.UUID;
14+
515
public final class Api extends JavaPlugin {
616

17+
private static Api instance;
18+
private static ServerVersion version;
19+
private Map<UUID, Button> buttons = new HashMap<>();
20+
721
@Override
822
public void onEnable() {
923
// Plugin startup logic
24+
instance = this;
25+
26+
String secIndicator = Bukkit.getBukkitVersion().split("\\.")[1];
27+
String preTrdIndicator = Bukkit.getBukkitVersion().split("\\.")[2];
28+
String trdIndicator = preTrdIndicator.split("-")[0];
1029

30+
if (Integer.parseInt(secIndicator) == 20) {
31+
if (Integer.parseInt(trdIndicator) >= 2) {
32+
version = ServerVersion.POST_1_20_2;
33+
} else {
34+
version = ServerVersion.PRE_1_20_2;
35+
}
36+
} else {
37+
if (Integer.parseInt(secIndicator) > 20) {
38+
version = ServerVersion.POST_1_20_2;
39+
} else {
40+
version = ServerVersion.PRE_1_20_2;
41+
}
42+
}
43+
44+
getCommand("spawnBtn").setExecutor(new SpawnButtonCommand());
45+
Bukkit.getPluginManager().registerEvents(new MoveListener(), this);
1146
}
1247

1348
@Override
1449
public void onDisable() {
1550
// Plugin shutdown logic
1651
}
52+
53+
public static Api getInstance() { return instance; }
54+
public Map<UUID, Button> getButtons() { return buttons; }
55+
public static ServerVersion getVersion() { return version; }
1756
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.littleprogrammer.guiapi;
2+
3+
public class GUI {
4+
5+
public GUI() {
6+
7+
}
8+
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.littleprogrammer.guiapi.commands;
2+
3+
import de.littleprogrammer.guiapi.components.Button;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandExecutor;
6+
import org.bukkit.command.CommandSender;
7+
import org.bukkit.entity.Player;
8+
9+
public class SpawnButtonCommand implements CommandExecutor {
10+
@Override
11+
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
12+
13+
new Button((Player) commandSender, "This is the \n text on the display", "localName");
14+
15+
16+
return false;
17+
}
18+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package de.littleprogrammer.guiapi.components;
2+
3+
import de.littleprogrammer.guiapi.Api;
4+
import de.littleprogrammer.guiapi.enums.ServerVersion;
5+
import de.littleprogrammer.guiapi.utils.Calculations;
6+
import de.littleprogrammer.guiapi.utils.TeleportInterpolator;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.Location;
9+
import org.bukkit.entity.Display;
10+
import org.bukkit.entity.EntityType;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.entity.TextDisplay;
13+
import org.bukkit.event.EventHandler;
14+
import org.bukkit.event.Listener;
15+
import org.bukkit.event.player.PlayerMoveEvent;
16+
import org.bukkit.util.Transformation;
17+
18+
import java.util.UUID;
19+
20+
public class Button implements Listener {
21+
22+
private String texture;
23+
private String localizedName;
24+
private UUID uuid;
25+
private TextDisplay textDisplay;
26+
private Location location;
27+
private Player player;
28+
29+
public Button(Player player, String texture, String localizedName) {
30+
this.player = player;
31+
this.texture = texture;
32+
this.localizedName = localizedName;
33+
uuid = UUID.randomUUID();
34+
35+
spawn();
36+
}
37+
38+
private void spawn() {
39+
Api.getInstance().getButtons().put(player.getUniqueId(), this);
40+
41+
textDisplay = (TextDisplay) player.getWorld().spawnEntity(Calculations.calculateInventoryLoc(player.getLocation()), EntityType.TEXT_DISPLAY);
42+
textDisplay.setText(texture);
43+
textDisplay.setGlowing(true);
44+
textDisplay.setBillboard(Display.Billboard.CENTER);
45+
textDisplay.setDisplayWidth(30);
46+
textDisplay.setDisplayHeight(30);
47+
Bukkit.getScheduler().runTaskLater(Api.getInstance(), () -> {
48+
49+
}, 2);
50+
}
51+
52+
public void updatePosition(Location playerLoc) {
53+
// textDisplay.setTeleportDuration(5);
54+
//
55+
location = Calculations.calculateInventoryLoc(playerLoc);
56+
// textDisplay.teleport(location);
57+
// textDisplay.setInterpolationDuration(5);
58+
// textDisplay.setInterpolationDelay(-1);
59+
// Transformation transformation = textDisplay.getTransformation();
60+
// transformation.getTranslation().set(location.toVector().subtract(textDisplay.getLocation().toVector()).toVector3f());
61+
// textDisplay.setTransformation(transformation);
62+
if (Api.getVersion().equals(ServerVersion.PRE_1_20_2)) {
63+
TeleportInterpolator teleportInterpolator = new TeleportInterpolator(textDisplay, location, 5, 1);
64+
teleportInterpolator.startInterpolation();
65+
} else {
66+
textDisplay.setTeleportDuration(5);
67+
textDisplay.teleport(location);
68+
}
69+
}
70+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package de.littleprogrammer.guiapi.components;
2+
3+
public class Text {
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.littleprogrammer.guiapi.enums;
2+
3+
public enum ServerVersion {
4+
PRE_1_20_2,
5+
POST_1_20_2;
6+
ServerVersion() {
7+
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package de.littleprogrammer.guiapi.listeners;
2+
3+
import de.littleprogrammer.guiapi.Api;
4+
import org.bukkit.event.EventHandler;
5+
import org.bukkit.event.Listener;
6+
import org.bukkit.event.player.PlayerMoveEvent;
7+
8+
public class MoveListener implements Listener {
9+
@EventHandler
10+
public void onPlayerMove(PlayerMoveEvent event) {
11+
if (Api.getInstance().getButtons().containsKey(event.getPlayer().getUniqueId())) {
12+
Api.getInstance().getButtons().get(event.getPlayer().getUniqueId()).updatePosition(event.getPlayer().getEyeLocation());
13+
}
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.littleprogrammer.guiapi.utils;
2+
3+
import org.bukkit.Location;
4+
5+
public class Calculations {
6+
7+
public static Location calculateInventoryLoc(Location midLocation) {
8+
double distance = 4; // 4 blocks away
9+
double yawRadians = Math.toRadians(midLocation.getYaw());
10+
double pitchRadians = Math.toRadians(midLocation.getPitch());
11+
double x = midLocation.getX() - distance * Math.sin(yawRadians);
12+
double y = midLocation.getY() - 1;
13+
double z = midLocation.getZ() + distance * Math.cos(yawRadians); // Negative here to match player rotation direction
14+
15+
return new Location(midLocation.getWorld(), x, y, z, midLocation.getYaw(), 0);
16+
}
17+
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.littleprogrammer.guiapi.utils;
2+
3+
import de.littleprogrammer.guiapi.Api;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.Location;
6+
import org.bukkit.entity.Entity;
7+
import org.bukkit.scheduler.BukkitTask;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class TeleportInterpolator {
13+
14+
private Entity entity;
15+
private Location targetLocation;
16+
private int steps;
17+
private int delay;
18+
19+
public TeleportInterpolator(Entity entity, Location targetLocation, int steps, int delay) {
20+
this.entity = entity;
21+
this.targetLocation = targetLocation;
22+
this.steps = steps;
23+
this.delay = delay;
24+
}
25+
26+
public void startInterpolation() {
27+
Location currentLocation = entity.getLocation();
28+
29+
double dx = (targetLocation.getX() - currentLocation.getX()) / steps;
30+
double dy = (targetLocation.getY() - currentLocation.getY()) / steps;
31+
double dz = (targetLocation.getZ() - currentLocation.getZ()) / steps;
32+
33+
for (int i = 1; i <= steps; i++) {
34+
double newX = currentLocation.getX() + dx * i;
35+
double newY = currentLocation.getY() + dy * i;
36+
double newZ = currentLocation.getZ() + dz * i;
37+
38+
Location intermediateLocation = new Location(targetLocation.getWorld(), newX, newY, newZ);
39+
40+
// Teleport the entity to the intermediate location after a delay
41+
teleportWithDelay(intermediateLocation, i * delay);
42+
}
43+
}
44+
45+
private void teleportWithDelay(Location location, int delayTicks) {
46+
Bukkit.getScheduler().runTaskLater(Api.getInstance(), () -> {
47+
entity.teleport(location);
48+
}, delayTicks);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)