Files
src
build.gradle
settings.gradle
Code
5 333
src/main/java/com/kodari/siftportal/SiftPortalPlugin.java
package com.kodari.siftportal;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Axis;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Orientable;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class SiftPortalPlugin extends JavaPlugin implements Listener, CommandExecutor {
private final Set<String> portalBlocks = new HashSet<>();
private final Map<UUID, LocationRecord> returnLocations = new HashMap<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
private World siftWorld;
private Material frameMaterial;
private Material portalMaterial;
@Override
public void onEnable() {
saveDefaultConfig();
frameMaterial = materialFromConfig("portal.frame-material", "OBSIDIAN");
portalMaterial = materialFromConfig("portal.portal-material", "NETHER_PORTAL");
if (frameMaterial == null || portalMaterial == null) {
getLogger().severe("Configured portal materials are unavailable; disabling SiftPortal.");
getServer().getPluginManager().disablePlugin(this);
return;
}
for (String portal : getConfig().getStringList("portals")) {
if (isValidPortalKey(portal)) {
portalBlocks.add(portal);
}
}
if (!ensureSiftWorld()) {
getLogger().severe("Could not load or create the Sift world; disabling SiftPortal.");
getServer().getPluginManager().disablePlugin(this);
return;
}
getServer().getPluginManager().registerEvents(this, this);
if (getCommand("sift") != null) {
getCommand("sift").setExecutor(this);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0 && args[0].equalsIgnoreCase("portal")) {
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cOnly players can create a portal."));
return true;
}
if (!player.hasPermission("siftportal.portal")) {
player.sendMessage(color("&cYou don't have permission to create a Sift portal."));
return true;
}
createPortal(player);
return true;
}
if (args.length > 0 && args[0].equalsIgnoreCase("create")) {
if (!sender.hasPermission("siftportal.portal")) {
sender.sendMessage(color("&cYou don't have permission to create the Sift dimension."));
return true;
}
sender.sendMessage(ensureSiftWorld()
? color("&aThe Sift dimension is ready.")
: color("&cThe Sift dimension could not be loaded."));
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cOnly players can travel to the Sift dimension."));
return true;
}
if (!player.hasPermission("siftportal.use")) {
player.sendMessage(color("&cYou don't have permission to use Sift portals."));
return true;
}
travel(player);
return true;
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (event.getTo() == null || sameBlock(event.getFrom(), event.getTo())) {
return;
}
Player player = event.getPlayer();
String portalKey = key(event.getTo().getBlock());
if (!portalBlocks.contains(portalKey)) {
return;
}
if (event.getTo().getBlock().getType() != portalMaterial) {
portalBlocks.remove(portalKey);
savePortalBlocks();
return;
}
long now = System.currentTimeMillis();
if (cooldowns.getOrDefault(player.getUniqueId(), 0L) > now) {
return;
}
if (player.hasPermission("siftportal.use")) {
travel(player);
}
}
private void travel(Player player) {
if (!ensureSiftWorld()) {
player.sendMessage(color("&cThe Sift dimension is unavailable."));
return;
}
boolean leavingSift = player.getWorld().getUID().equals(siftWorld.getUID());
org.bukkit.Location destination;
if (leavingSift) {
LocationRecord returnLocation = returnLocations.remove(player.getUniqueId());
destination = returnLocation == null ? returnWorldSpawn() : returnLocation.toLocation();
if (destination == null) {
player.sendMessage(color("&cThe return world is unavailable."));
return;
}
} else {
returnLocations.put(player.getUniqueId(), LocationRecord.from(player.getLocation()));
destination = safeSpawn(siftWorld);
}
int cooldownSeconds = Math.max(0, getConfig().getInt("portal.cooldown-seconds", 3));
if (!player.teleport(destination)) {
return;
}
cooldowns.put(player.getUniqueId(), System.currentTimeMillis() + cooldownSeconds * 1000L);
playEffects(player, destination);
player.sendMessage(leavingSift
? color("&aYou returned from the Sift dimension.")
: color("&aYou entered the Sift dimension."));
}
private void createPortal(Player player) {
boolean spansX = player.getFacing() == BlockFace.NORTH || player.getFacing() == BlockFace.SOUTH;
int centerX = player.getLocation().getBlockX();
int baseY = player.getLocation().getBlockY();
int centerZ = player.getLocation().getBlockZ();
Axis portalAxis = spansX ? Axis.X : Axis.Z;
for (int width = -1; width <= 2; width++) {
for (int height = 0; height <= 4; height++) {
int x = centerX + (spansX ? width : 0);
int y = baseY + height;
int z = centerZ + (spansX ? 0 : width);
Block block = player.getWorld().getBlockAt(x, y, z);
boolean frame = width == -1 || width == 2 || height == 0 || height == 4;
if (frame) {
block.setType(frameMaterial, false);
} else {
block.setType(portalMaterial, false);
BlockData data = block.getBlockData();
if (data instanceof Orientable orientable) {
orientable.setAxis(portalAxis);
block.setBlockData(orientable, false);
}
portalBlocks.add(key(block));
}
}
}
savePortalBlocks();
player.sendMessage(color("&aSift portal created. Walk through it to travel."));
}
private boolean ensureSiftWorld() {
if (siftWorld != null) {
return true;