Skip to content

Commit f6ff359

Browse files
Texts done
- Added functionality to texts - Added option to change size to button and text
1 parent 67c8313 commit f6ff359

File tree

5 files changed

+73
-27
lines changed

5 files changed

+73
-27
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public List<Component> getComponents() {
9393
return new ArrayList<>(components.values());
9494
}
9595

96+
public int getButtonAmount() {
97+
return buttons.size();
98+
}
99+
96100
public boolean isOpen() {
97101
return open;
98102
}
@@ -107,10 +111,13 @@ public SimpleGui addButton(Button button) {
107111
}
108112

109113
public SimpleGui addContent(Text content) {
110-
if (content != null) {
111-
content.remove();
114+
if (this.content != null) {
115+
components.remove(this.content.getUniqueId());
116+
this.content.remove();
112117
}
113118
this.content = content;
119+
components.put(content.getUniqueId(), content);
120+
content.setGui(this);
114121
return this;
115122
}
116123
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
public class SpawnCommand implements CommandExecutor {
1212
@Override
1313
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
14-
Button button = new Button((Player) commandSender, "\uE001", "\uE002", "LcoalizedName", 1).onClick(event -> {
14+
Button button = new Button("\uE001", "\uE002", 1).setSize(2).onClick(event -> {
1515
event.getPlayer().sendMessage("You clicked a button1");
1616
});
1717

18-
Button button2 = new Button((Player) commandSender, "\uE001", "\uE002", "LcoalizedName", 2).onClick(event -> {
18+
Button button2 = new Button("\uE001", "\uE002", 2).onClick(event -> {
1919
event.getPlayer().sendMessage("You clicked a button2");
2020
});
2121

22-
Button button3 = new Button((Player) commandSender, "\uE001", "\uE002", "LcoalizedName", 3).onClick(event -> {
22+
Button button3 = new Button("\uE001", "\uE002", 3).onClick(event -> {
2323
event.getPlayer().sendMessage("You clicked a button3");
2424
});
2525

26-
SimpleGui gui = new SimpleGui("Some title", 1, 1).addContent(new Text("Test")).addButton(button).addButton(button2).addButton(button3);
26+
SimpleGui gui = new SimpleGui("Some title", 1, 1).addContent(new Text("Test").setSize(2)).addButton(button).addButton(button2).addButton(button3);
2727
gui.open((Player) commandSender);
2828

2929
return false;

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,26 @@ public class Button implements Component {
1919

2020
private String texture;
2121
private String hoverTexture;
22-
private String localizedName;
2322
private UUID uuid;
2423
private TextDisplay textDisplay;
2524
private Location location;
26-
private Player player;
2725
private Consumer<PlayerInteractEntityEvent> clickAction;
2826
private Consumer<HoverButtonEvent> hoverAction;
2927
private Consumer<UnHoverButtonEvent> unHoverAction;
3028
private int slot;
29+
private int size = 2;
3130
private SimpleGui simpleGui;
3231

33-
public Button(Player player, String texture, String hoverTexture, String localizedName, int slot) {
34-
this.player = player;
32+
public Button(String texture, String hoverTexture, int slot) {
3533
this.texture = texture;
3634
this.hoverTexture = hoverTexture;
37-
this.localizedName = localizedName;
3835
this.slot = slot;
3936

4037
uuid = UUID.randomUUID();
4138
}
4239

4340
public void spawn() {
44-
textDisplay = (TextDisplay) player.getWorld().spawnEntity(Calculations.calculateInventoryCenter(player.getLocation()), EntityType.TEXT_DISPLAY);
41+
textDisplay = (TextDisplay) simpleGui.getCenterLocation().getWorld().spawnEntity(Calculations.calculateComponentLocation(simpleGui, this, simpleGui.getButtonAmount()), EntityType.TEXT_DISPLAY);
4542
textDisplay.setCustomName(uuid.toString());
4643
textDisplay.setCustomNameVisible(false);
4744
textDisplay.setText(texture);
@@ -52,7 +49,7 @@ public void spawn() {
5249
textDisplay.setVisibleByDefault(false);
5350
textDisplay.setDefaultBackground(false);
5451
Transformation transformation = textDisplay.getTransformation();
55-
transformation.getScale().set(new Vector3f(2, 2, 2));
52+
transformation.getScale().set(new Vector3f(size, size, size));
5653
textDisplay.setTransformation(transformation);
5754
}
5855

@@ -140,4 +137,8 @@ public int getSlot() {
140137
public void setGui(SimpleGui gui) {
141138
simpleGui = gui;
142139
}
140+
public Button setSize(int size) {
141+
this.size = size;
142+
return this;
143+
}
143144
}
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,87 @@
11
package de.littleprogrammer.guiapi.components;
22

3-
import org.bukkit.entity.Display;
4-
import org.bukkit.entity.Entity;
5-
import org.bukkit.entity.Player;
3+
import de.littleprogrammer.guiapi.GuiApi;
4+
import de.littleprogrammer.guiapi.SimpleGui;
5+
import org.bukkit.entity.*;
6+
import org.bukkit.util.Transformation;
7+
import org.joml.Vector3f;
68

79
import java.util.UUID;
810

911
public class Text implements Component {
1012

13+
private String text;
14+
private UUID uuid;
15+
private int size = 2;
16+
private TextDisplay textDisplay;
17+
private SimpleGui simpleGui;
18+
1119
public Text(String text) {
20+
this.text = text;
21+
uuid = UUID.randomUUID();
22+
}
1223

24+
@Override
25+
public void spawn() {
26+
textDisplay = (TextDisplay) simpleGui.getCenterLocation().getWorld().spawnEntity(simpleGui.getCenterLocation(), EntityType.TEXT_DISPLAY);
27+
textDisplay.setCustomName(uuid.toString());
28+
textDisplay.setCustomNameVisible(false);
29+
textDisplay.setText(text);
30+
textDisplay.setGlowing(true);
31+
textDisplay.setBillboard(Display.Billboard.CENTER);
32+
textDisplay.setDisplayWidth(30);
33+
textDisplay.setDisplayHeight(30);
34+
textDisplay.setVisibleByDefault(false);
35+
textDisplay.setDefaultBackground(false);
36+
Transformation transformation = textDisplay.getTransformation();
37+
transformation.getScale().set(new Vector3f(size, size, size));
38+
textDisplay.setTransformation(transformation);
1339
}
1440

1541
@Override
1642
public Entity getEntity() {
17-
return null;
43+
return textDisplay;
1844
}
1945

2046
@Override
2147
public Display getDisplay() {
22-
return null;
48+
return textDisplay;
2349
}
2450

2551
@Override
2652
public UUID getUniqueId() {
27-
return null;
53+
return uuid;
2854
}
2955

30-
@Override
31-
public void show(Player player) {
56+
public SimpleGui getGui() {
57+
return simpleGui;
58+
}
3259

60+
public int getSize() {
61+
return size;
3362
}
3463

35-
@Override
36-
public void hide(Player player) {
64+
public void setGui(SimpleGui simpleGui) {
65+
this.simpleGui = simpleGui;
66+
}
3767

68+
public Text setSize(int size) {
69+
this.size = size;
70+
return this;
3871
}
3972

4073
@Override
41-
public void remove() {
42-
74+
public void show(Player player) {
75+
player.showEntity(GuiApi.getInstance().getPlugin(), textDisplay);
4376
}
4477

4578
@Override
46-
public void spawn() {
79+
public void hide(Player player) {
80+
player.hideEntity(GuiApi.getInstance().getPlugin(), textDisplay);
81+
}
4782

83+
@Override
84+
public void remove() {
85+
textDisplay.remove();
4886
}
4987
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Location calculateComponentLocation(SimpleGui simpleGui, Component
5050
}
5151
} else {
5252
//Is content
53-
return centerLoc.clone().add(0, 1, 0);
53+
return centerLoc.clone().add(0, 0.5, 0);
5454
}
5555
return null;
5656
}

0 commit comments

Comments
 (0)