Skip to content

Commit

Permalink
🐛 fix(DimensionalHome): Fix #27, Make sure the player can go back home.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfElements committed Feb 24, 2021
1 parent dd1aba6 commit 044b15b
Showing 1 changed file with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,58 @@ public ItemUseHandler onRightClick() {

@Override
public void onRightClick(PlayerRightClickEvent e) {
Location playerPrevLocation = e.getPlayer().getLocation();



if (!idSet) {
e.getItem().setItemMeta(updateLore(e.getItem(), e.getPlayer()));
idSet = true;
}
if (e.getPlayer().getWorld() != Bukkit.getServer().getWorld("dimensionalhome") && idSet) {
playerPrevLocation = e.getPlayer().getLocation();

if (doesntContainNewChunkID(e.getItem())) {
idSet = false;
}

if(e.getPlayer().getWorld() != Bukkit.getServer().getWorld("dimensionalhome")) {
PaperLib.teleportAsync(e.getPlayer(), new Location(Bukkit.getServer().getWorld("dimensionalhome"), 16 * PersistentDataAPI.getInt(e.getItem().getItemMeta(), chunkId) + 8, 65, 16 * 0 + 8));
} else if (idSet) {

if (doesntContainNewChunkID(e.getItem())) {
idSet = false;
}

PaperLib.teleportAsync(e.getPlayer(), playerPrevLocation.getWorld() != Bukkit.getServer().getWorld("dimensionalhome") ? playerPrevLocation : e.getPlayer().getBedSpawnLocation() != null ? e.getPlayer().getBedSpawnLocation() : e.getPlayer().getServer().getWorld("world").getSpawnLocation());
} else {
PaperLib.teleportAsync(e.getPlayer(), e.getPlayer().getBedSpawnLocation() == null ? e.getPlayer().getBedSpawnLocation() : Bukkit.getServer().getWorld("world").getSpawnLocation());
updateLore(e.getItem(), e.getPlayer());
}

e.cancel();
}
};
}

protected boolean doesntContainNewChunkID(ItemStack item) {
ItemMeta im = item.getItemMeta();

if (!im.hasLore()) {
throw new IllegalArgumentException("This item does not have any lore!");
}

List<String> lore = im.getLore();

for (int line = 0; line < lore.size(); line++ ) {
if (lore.get(line).contains("CHUNK ID: <id>")) {
return true;
}

}

return false;
}

public int getChunkId() {
return id;
}

protected ItemMeta updateLore(ItemStack item, Player p) {
protected void updateLore(ItemStack item, Player p) {
ItemMeta im = item.getItemMeta();

if (!im.hasLore()) {
Expand All @@ -71,15 +101,14 @@ protected ItemMeta updateLore(ItemStack item, Player p) {
if (lore.get(line).contains("CHUNK ID: <id>")) {
id++;
lore.set(line, lore.get(line).replace("<id>", String.valueOf(id)));
PersistentDataAPI.setInt(this.getItem().getItemMeta(), chunkId, id);

PersistentDataAPI.setInt(im, chunkId, id);

idSet = true;
}

}

im.setLore(lore);

}

return im;

item.setItemMeta(im);
}
}

0 comments on commit 044b15b

Please sign in to comment.