Skip to content

Commit

Permalink
new feature: right click on compasses in item frames to teleport
Browse files Browse the repository at this point in the history
  • Loading branch information
percyqaz committed Jul 2, 2023
1 parent 6e8a6d0 commit e34cf76
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
*.iml
target
72 changes: 70 additions & 2 deletions src/main/java/me/Percyqaz/Lodestone/CompassListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import org.bukkit.*;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
Expand All @@ -28,6 +29,7 @@ public class CompassListener implements Listener
boolean enableDimensionalTravel;
boolean teleportationConsumesCompass;
boolean allowUsingCompassFromInventory;
boolean allowUsingCompassFromItemFrame;
Map<String, Long> cooldowns = new HashMap<>();

public CompassListener(Lodestone plugin, FileConfiguration config)
Expand All @@ -42,6 +44,7 @@ public CompassListener(Lodestone plugin, FileConfiguration config)
enableDimensionalTravel = config.getBoolean("enableDimensionalTravel", true);
teleportationConsumesCompass = config.getBoolean("teleportationConsumesCompass", true);
allowUsingCompassFromInventory = config.getBoolean("allowUsingCompassFromInventory", false);
allowUsingCompassFromItemFrame = config.getBoolean("allowUsingCompassFromItemFrame", false);

// Cooldown must always be at least as long as warmup
cooldownTimeMillis = Math.max(cooldownTimeMillis, warmupTimeTicks * 50L);
Expand Down Expand Up @@ -354,4 +357,69 @@ else if (itemType == Material.COMPASS)
);
}
}

@EventHandler(priority = EventPriority.LOW)
public void PlayerInteractEntity(PlayerInteractEntityEvent e)
{
if (!allowUsingCompassFromItemFrame)
{
return;
}

Entity clicked = e.getRightClicked();
if (clicked.getType() != EntityType.ITEM_FRAME && clicked.getType() != EntityType.GLOW_ITEM_FRAME)
{
return;
}

ItemFrame itemFrame = (ItemFrame)clicked;
ItemStack item = itemFrame.getItem();
Material itemType = item.getType();

if (itemType != Material.COMPASS)
{
return;
}

CompassMeta itemMeta = (CompassMeta) item.getItemMeta();
if (!itemMeta.hasLodestone() || !itemMeta.isLodestoneTracked())
{
return;
}

e.setCancelled(true);
Player player = e.getPlayer();

Location oldLoc = player.getLocation();

if (!CheckPlayerDimension(player, itemMeta.getLodestone()))
{
player.sendMessage(config.getString("teleportFailedDifferentDimension"));
return;
}

int cooldown = CheckPlayerCooldown(player.getName());
if (cooldown > 0)
{
player.sendMessage(config.getString("teleportFailedWaitForCooldown").replace("%cooldown%", String.valueOf(cooldown)));
return;
}

String teleportMessage =
itemMeta.hasDisplayName()
? config.getString("teleportSucceededNamedLocation").replace("%location%", itemMeta.getDisplayName())
: config.getString("teleportSucceeded");
player.sendMessage(teleportMessage);
player.spawnParticle(Particle.CLOUD, oldLoc.add(0.0f, 1.0f, 0.0f), 50, 0.5f, 1.0f, 0.5f, 0.01f);

Location pos = itemMeta.getLodestone();
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(
plugin,
() -> {
player.teleport(pos.add(0.5, 1.5, 0.5));
player.spawnParticle(Particle.CLOUD, player.getLocation().add(0.0f, 1.0f, 0.0f), 50, 0.5f, 1.0f, 0.5f, 0.01f);
player.playSound(player.getLocation(), Sound.BLOCK_CONDUIT_ACTIVATE, 1.0f, 0.5f);
}
);
}
}
1 change: 1 addition & 0 deletions src/main/java/me/Percyqaz/Lodestone/Lodestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void onEnable() {
config.addDefault("enableDimensionalTravel", true);
config.addDefault("teleportationConsumesCompass", true);
config.addDefault("allowUsingCompassFromInventory", false);
config.addDefault("allowUsingCompassFromItemFrame", false);

config.options().copyDefaults(true);
saveConfig();
Expand Down

0 comments on commit e34cf76

Please sign in to comment.