Skip to content

Commit cbe1ed5

Browse files
Nice progress
- Made the gui buttons align on a circle around the player, so they are always in front of the player - Changed it back to a plugin, to make testing easier - Made the click check work (Might want to change the event to something where I can get the left and th right click) - Clicking on a button is working now
1 parent 008b4c8 commit cbe1ed5

File tree

7 files changed

+131
-29
lines changed

7 files changed

+131
-29
lines changed

build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,23 @@ 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

3741
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
3842
options.release.set(targetJavaVersion)
3943
}
4044
}
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/GuiApi.java

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

3+
import de.littleprogrammer.guiapi.commands.SpawnCommand;
34
import de.littleprogrammer.guiapi.enums.ServerVersion;
45
import de.littleprogrammer.guiapi.listeners.GuiEvents;
56
import de.littleprogrammer.guiapi.listeners.MoveListener;
7+
import org.bukkit.Bukkit;
68
import org.bukkit.entity.Player;
79
import org.bukkit.event.Listener;
810
import org.bukkit.plugin.java.JavaPlugin;
@@ -12,17 +14,26 @@
1214
import java.util.Map;
1315
import java.util.UUID;
1416

15-
public final class GuiApi {
17+
public final class GuiApi extends JavaPlugin {
1618

17-
private final JavaPlugin plugin;
19+
private JavaPlugin plugin;
1820
private static GuiApi instance;
1921
private ServerVersion version;
2022
private final Listener listener = new GuiEvents();
2123
private Map<UUID, SimpleGui> guis = new HashMap<>();
2224

23-
public GuiApi(JavaPlugin plugin) {
24-
this.plugin = plugin;
25+
@Override
26+
public void onEnable() {
27+
// Plugin startup logic
28+
this.plugin = this;
2529
instance = this;
30+
31+
init();
32+
}
33+
34+
@Override
35+
public void onDisable() {
36+
// Plugin shutdown logic
2637
}
2738

2839
public void init() {
@@ -44,8 +55,10 @@ public void init() {
4455
}
4556
}
4657

47-
this.plugin.getServer().getPluginManager().registerEvents(this.listener, this.plugin);
48-
this.plugin.getServer().getPluginManager().registerEvents(new MoveListener(), this.plugin);
58+
Bukkit.getPluginManager().registerEvents(this.listener, this.plugin);
59+
Bukkit.getPluginManager().registerEvents(new MoveListener(), this.plugin);
60+
61+
getCommand("spawnGui").setExecutor(new SpawnCommand());
4962
}
5063

5164
public JavaPlugin getPlugin() {return this.plugin;}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package de.littleprogrammer.guiapi.commands;
2+
3+
import de.littleprogrammer.guiapi.SimpleGui;
4+
import de.littleprogrammer.guiapi.components.Button;
5+
import de.littleprogrammer.guiapi.components.Text;
6+
import org.bukkit.command.Command;
7+
import org.bukkit.command.CommandExecutor;
8+
import org.bukkit.command.CommandSender;
9+
import org.bukkit.entity.Player;
10+
11+
public class SpawnCommand implements CommandExecutor {
12+
@Override
13+
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
14+
Button button = new Button((Player) commandSender, "\uE001", "LcoalizedName", 1).onClick(event -> {
15+
event.getPlayer().sendMessage("You clicked a button1");
16+
});
17+
18+
Button button2 = new Button((Player) commandSender, "\uE001", "LcoalizedName", 2).onClick(event -> {
19+
event.getPlayer().sendMessage("You clicked a button2");
20+
});
21+
22+
Button button3 = new Button((Player) commandSender, "\uE001", "LcoalizedName", 3).onClick(event -> {
23+
event.getPlayer().sendMessage("You clicked a button3");
24+
});
25+
26+
SimpleGui gui = new SimpleGui("Some title", 1, 1).addContent(new Text("Test")).addButton(button).addButton(button2).addButton(button3);
27+
gui.open((Player) commandSender);
28+
29+
return false;
30+
}
31+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.bukkit.Location;
77
import org.bukkit.entity.*;
88
import org.bukkit.event.player.PlayerInteractEntityEvent;
9+
import org.bukkit.util.Transformation;
10+
import org.joml.Vector3f;
911

1012
import javax.annotation.Nonnull;
1113
import java.util.UUID;
@@ -41,6 +43,10 @@ public void spawn() {
4143
textDisplay.setDisplayWidth(30);
4244
textDisplay.setDisplayHeight(30);
4345
textDisplay.setVisibleByDefault(false);
46+
textDisplay.setDefaultBackground(false);
47+
Transformation transformation = textDisplay.getTransformation();
48+
transformation.getScale().set(new Vector3f(2, 2, 2));
49+
textDisplay.setTransformation(transformation);
4450
}
4551

4652
public void show(Player player) {

src/main/java/de/littleprogrammer/guiapi/listeners/GuiEvents.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,43 @@
44
import de.littleprogrammer.guiapi.SimpleGui;
55
import de.littleprogrammer.guiapi.components.Button;
66
import de.littleprogrammer.guiapi.components.Component;
7+
import de.littleprogrammer.guiapi.utils.Calculations;
8+
import org.bukkit.entity.Display;
9+
import org.bukkit.entity.Entity;
710
import org.bukkit.event.EventHandler;
811
import org.bukkit.event.Listener;
12+
import org.bukkit.event.player.PlayerAnimationEvent;
913
import org.bukkit.event.player.PlayerInteractEntityEvent;
1014

1115
import java.util.UUID;
1216

1317
public class GuiEvents implements Listener {
1418

15-
@EventHandler
16-
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
19+
@EventHandler(ignoreCancelled = true)
20+
public void onPlayerAnimation(PlayerAnimationEvent event) {
1721
final SimpleGui simpleGui = GuiApi.getInstance().getGUI(event.getPlayer());
22+
System.out.println("Fired click event");
1823

1924
if (simpleGui == null) return;
2025

21-
UUID uuid = event.getRightClicked().getUniqueId();
26+
Entity awaitenEntity = null;
27+
for (Entity entity : event.getPlayer().getNearbyEntities(7, 7, 7)) {
28+
if (entity instanceof Display && entity.getCustomName() != null) {
29+
if (Calculations.playerLookingAtEntity(event.getPlayer(), entity)) {
30+
awaitenEntity = entity;
31+
break;
32+
}
33+
}
34+
}
35+
36+
if (awaitenEntity == null) { return; }
37+
38+
UUID uuid = UUID.fromString(awaitenEntity.getCustomName());
2239
Component component = simpleGui.getComponent(uuid);
2340
if (!(component instanceof Button)) return;
2441

25-
((Button) component).getClickAction().accept(event);
42+
Button button = (Button) component;
43+
System.out.println("Click on button: " + button.getUniqueId());
44+
button.getClickAction().accept(new PlayerInteractEntityEvent(event.getPlayer(), button.getEntity()));
2645
}
2746
}

src/main/java/de/littleprogrammer/guiapi/utils/Calculations.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import de.littleprogrammer.guiapi.components.Button;
55
import de.littleprogrammer.guiapi.components.Component;
66
import org.bukkit.Location;
7+
import org.bukkit.entity.Display;
8+
import org.bukkit.entity.Entity;
9+
import org.bukkit.entity.Player;
10+
import org.bukkit.util.Vector;
711

812
public class Calculations {
913

@@ -27,7 +31,7 @@ public static Location calculateComponentLocation(SimpleGui simpleGui, Component
2731
//Is button in row
2832
switch (buttonAmount) {
2933
case 1:
30-
return centerLoc.clone().subtract(0, 0.5, 0);
34+
return centerLoc.clone().subtract(0, 0, 0);
3135
case 2:
3236
if (button.getSlot() == 1) {
3337
return locations[0];
@@ -39,7 +43,7 @@ public static Location calculateComponentLocation(SimpleGui simpleGui, Component
3943
if (button.getSlot() == 1) {
4044
return locations[0];
4145
} else if (button.getSlot() == 2) {
42-
return centerLoc.clone().subtract(0, 0.5, 0);
46+
return centerLoc.clone().subtract(0, 0, 0);
4347
} else if (button.getSlot() == 3) {
4448
return locations[1];
4549
}
@@ -52,28 +56,36 @@ public static Location calculateComponentLocation(SimpleGui simpleGui, Component
5256
return null;
5357
}
5458

55-
private static Location[] calculateTrianglePoints(Location midLocation, Location centerLocation) {
56-
// Calculate distance between mid and center locations
57-
double distance = midLocation.distance(centerLocation);
59+
/**
60+
* @param playerLocation the location of the player (the point in the middle)
61+
* @param centerLocation the location on the circle to get the correct height
62+
*/
63+
private static Location[] calculateTrianglePoints(Location playerLocation, Location centerLocation) {
64+
double radius = playerLocation.distance(centerLocation);
5865

59-
// Calculate angle between mid and center locations
60-
double angle = Math.atan2(centerLocation.getZ() - midLocation.getZ(), centerLocation.getX() - midLocation.getX());
66+
Vector vector1 = playerLocation.getDirection().setY(0).normalize().multiply(radius).rotateAroundY(Math.toRadians(30));
67+
Vector vector2 = playerLocation.getDirection().setY(0).normalize().multiply(radius).rotateAroundY(Math.toRadians(-30));
6168

62-
// Calculate offset angles for left and right points
63-
double leftOffsetAngle = angle + Math.PI / 2; // 90 degrees counterclockwise
64-
double rightOffsetAngle = angle - Math.PI / 2; // 90 degrees clockwise
69+
Location loc1 = playerLocation.clone().add(vector1);
70+
loc1.setY(centerLocation.getY());
6571

66-
// Calculate left and right points using offset angles and distance
67-
Location leftPoint = calculatePointOnCircle(centerLocation, leftOffsetAngle, 4);
68-
Location rightPoint = calculatePointOnCircle(centerLocation, rightOffsetAngle, 4);
72+
Location loc2 = playerLocation.clone().add(vector2);
73+
loc2.setY(centerLocation.getY());
6974

70-
return new Location[]{leftPoint, rightPoint};
75+
return new Location[]{loc1, loc2};
7176
}
7277

73-
private static Location calculatePointOnCircle(Location centerLocation, double angle, double radius) {
74-
double x = centerLocation.getX() + radius * Math.cos(angle);
75-
double z = centerLocation.getZ() + radius * Math.sin(angle);
76-
return new Location(centerLocation.getWorld(), x, centerLocation.getY(), z, centerLocation.getYaw(), centerLocation.getPitch());
77-
}
78+
public static boolean playerLookingAtEntity(Player player, Entity entity) {
79+
Vector playerDirection = player.getLocation().getDirection().normalize();
80+
81+
Location entityLocation = entity.getLocation();
82+
Location playerEyeLocation = player.getEyeLocation();
83+
84+
Vector playerToEntity = entityLocation.toVector().subtract(playerEyeLocation.toVector()).normalize();
85+
double dotProduct = playerDirection.dot(playerToEntity);
7886

87+
//System.out.println("Checking entity" + entity + " " + entity.getCustomName() + " dot: " + dotProduct);
88+
89+
return dotProduct > 0.97;
90+
}
7991
}

src/main/resources/plugin.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: 3dGuiApi
2+
version: '${version}'
3+
main: de.littleprogrammer.guiapi.GuiApi
4+
api-version: '1.19'
5+
authors: [LittleProgrammer]
6+
description: An API to make 3D Gui's in minecraft
7+
commands:
8+
spawnGui:

0 commit comments

Comments
 (0)