Skip to content

Commit

Permalink
make own inventory clickable and icon support *wip*
Browse files Browse the repository at this point in the history
  • Loading branch information
UsainSrht committed Jan 1, 2024
1 parent c58a7ec commit 1021352
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/main/java/me/usainsrht/uhomes/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import me.usainsrht.uhomes.util.MessageUtil;
import me.usainsrht.uhomes.util.NBTUtil;
import me.usainsrht.uhomes.util.SoundUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Formatter;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -203,6 +205,12 @@ public CompletableFuture<Boolean> canRegisterHome(UUID uuid) {
}

public void teleport(Entity entity, Home home) {
if (entity.getWorld() != home.getLocation().getWorld() && !entity.hasPermission(MainConfig.getTpBetweenWorldsPerm())) {
MessageUtil.send(entity, MainConfig.getMessage("teleport_between_worlds_permission"),
Placeholder.unparsed("permission", MainConfig.getTpBetweenWorldsPerm()));
SoundUtil.play(entity, MainConfig.getSound("teleport_between_worlds_permission"));
return;
}
TimedTeleport timedTeleport = new TimedTeleport()
.entity(entity)
.location(home.getLocation())
Expand Down Expand Up @@ -255,6 +263,22 @@ public void delete(Player player, Home home) {
List<Home> homes = loadedHomes.get(uuid);
homes.remove(home);
}
NBTFile nbtFile = getNBTFile(uuid);
NBTCompoundList compoundList = nbtFile.getCompoundList("Homes");
for (int i = 0; i < compoundList.size(); i++) {
NBTListCompound current = compoundList.get(i);
if (home.getCreated() == current.getLong("Created")) {
compoundList.remove(i);
try {
nbtFile.save();
} catch (IOException e) {
plugin.getLogger().severe("An error occurred while deleting home of "+Bukkit.getOfflinePlayer(uuid).getName()+" ("+uuid+")");
e.printStackTrace();
}
return;
}
}

}

public void rename(Player player, Home home) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/me/usainsrht/uhomes/config/MainConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public static void create(ConfigurationSection config) {
noHomeItem = config.getConfigurationSection("gui.no_home");
setHomeItem = config.getConfigurationSection("gui.sethome");

homeButtonLeftClick = HomeButtonAction.valueOf(config.getString("left_click"));
homeButtonRightClick = HomeButtonAction.valueOf(config.getString("right_click"));
homeButtonLeftClickWithShift = HomeButtonAction.valueOf(config.getString("left_click_with_shift"));
homeButtonRightClickWithShift = HomeButtonAction.valueOf(config.getString("right_click_with_shift"));
homeButtonLeftClick = HomeButtonAction.valueOf(config.getString("gui.left_click"));
homeButtonRightClick = HomeButtonAction.valueOf(config.getString("gui.right_click"));
homeButtonLeftClickWithShift = HomeButtonAction.valueOf(config.getString("gui.left_click_with_shift"));
homeButtonRightClickWithShift = HomeButtonAction.valueOf(config.getString("gui.right_click_with_shift"));

setHomeGuiTitle = config.getString("anvil_gui.sethome.title");
setHomeGuiTitleNotValid = config.getString("anvil_gui.sethome.home_name_not_valid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.usainsrht.uhomes.command.SetHomeCommand;
import me.usainsrht.uhomes.gui.HomeButtonAction;
import me.usainsrht.uhomes.gui.HomesGUI;
import me.usainsrht.uhomes.util.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -33,9 +34,8 @@ public void onClick(InventoryClickEvent e) {
if (!player.hasMetadata("HomesGUI") || player.getMetadata("HomesGUI").isEmpty()) return;
Inventory homesGUI = (Inventory) player.getMetadata("HomesGUI").get(0).value();

InventoryView inventoryView = e.getView();
Inventory inventory = inventoryView.getTopInventory();
if (inventory != homesGUI) return;
Inventory inventory = e.getClickedInventory();
if (inventory == null || inventory != homesGUI) return;
e.setCancelled(true);

ItemStack item = e.getCurrentItem();
Expand All @@ -49,6 +49,12 @@ public void onClick(InventoryClickEvent e) {
.filter(home -> home.getCreated() == homeCompound.getLong("Created"))
.findFirst();
optionalHome.ifPresent(home -> {
ItemStack cursor = e.getCursor();
if (!cursor.getType().isAir()) {
home.setIcon(cursor.clone());
HomesGUI.open(home.getOwner(), player);
return;
}
HomeButtonAction action = HomeButtonAction.getFromClick(e.getClick());
if (action == null) return;
switch (action) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ messages:
- "<yellow>⚠ Don't move for <#FFC355><italic><seconds> seconds</italic><gray>."
teleport_cancel: "<dark_red><bold>🚫</bold> <red>You moved and cancelled the teleport."
teleport_tick: ""
teleport_between_worlds_permission: "<red>You don't have <underlined><permission></underlined> permission!"

sounds:
reload: entity.villager.yes
Expand All @@ -36,6 +37,7 @@ sounds:
teleport_start: block.portal.trigger
teleport_cancel: entity.ender_eye.death
teleport_tick: ui.button.click
teleport_between_worlds_permission: entity.villager.no

# permission to calculate home limit of a player
# for example if player has a "home.limit.5" permission he/she
Expand Down

0 comments on commit 1021352

Please sign in to comment.