|
| 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