Files
src
build.gradle
Code
3 776
src/main/java/com/kodari/chronorelics/ChronoRelicsPlugin.java
package com.kodari.chronorelics;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.particles.XParticle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
public final class ChronoRelicsPlugin extends org.bukkit.plugin.java.JavaPlugin implements Listener, TabExecutor {
private NamespacedKey relicKey;
private NamespacedKey ruinedChunkKey;
private NamespacedKey ghostExpiryKey;
private World presentWorld;
private World pastWorld;
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
private final Map<UUID, Long> portalCooldowns = new HashMap<>();
private final Map<World, Location> timelinePortals = new HashMap<>();
private final Set<UUID> glidingPlayers = new HashSet<>();
@Override
public void onEnable() {
relicKey = new NamespacedKey(this, "relic");
ruinedChunkKey = new NamespacedKey(this, "ruined_chunk");
ghostExpiryKey = new NamespacedKey(this, "ghost_expiry");
presentWorld = Bukkit.getWorlds().get(0);
pastWorld = Bukkit.getWorld("chrono_past");
if (pastWorld == null) {
pastWorld = Bukkit.createWorld(new WorldCreator("chrono_past")
.environment(World.Environment.NORMAL)
.seed(presentWorld.getSeed()));
}
getServer().getPluginManager().registerEvents(this, this);
getCommand("chronorelics").setExecutor(this);
getCommand("timeline").setExecutor(this);
getCommand("timeline").setTabCompleter(this);
for (org.bukkit.Chunk chunk : presentWorld.getLoadedChunks()) {
removeGeneratedRubble(chunk);
}
loadTimelinePortal(presentWorld, "portals.present");
loadTimelinePortal(pastWorld, "portals.past");
getServer().getScheduler().runTaskTimer(this, this::animateTimelinePortals, 10L, 10L);
getServer().getScheduler().runTaskTimer(this, this::triggerAutomaticTimeSlip, 6000L, 6000L);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("timeline")) {
if (args.length == 1 && args[0].equalsIgnoreCase("portal")) {
if (!sender.hasPermission("chronorelics.admin")) {
sender.sendMessage(color("&cYou do not have permission to create timeline portals."));
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cOnly players can choose where to create the portals."));
return true;
}
int x = player.getLocation().getBlockX();
int z = player.getLocation().getBlockZ();
createTimelinePortal(presentWorld, x, z);
createTimelinePortal(pastWorld, x, z);
saveTimelinePortal(presentWorld, "portals.present");
saveTimelinePortal(pastWorld, "portals.past");
saveConfig();
player.sendMessage(color("&bPaired timeline portals created near &f" + x + ", " + z
+ "&b in the Past and Present."));
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cOnly players can travel between timelines."));
return true;
}
if (args.length == 1 && args[0].equalsIgnoreCase("find")) {
Location portal = timelinePortals.get(player.getWorld());
if (portal == null) {
player.sendMessage(color("&cThere is no timeline rift in this world."));
return true;
}
player.setCompassTarget(portal);
int distance = (int) Math.sqrt(player.getLocation().distanceSquared(portal));
player.sendMessage(color("&bThe hidden rift is at &f" + portal.getBlockX() + ", "
+ portal.getBlockY() + ", " + portal.getBlockZ() + "&b. Your compass points toward it (&f"
+ distance + " blocks&b)."));
return true;
}
if (args.length != 1 || !(args[0].equalsIgnoreCase("past") || args[0].equalsIgnoreCase("present"))) {
sender.sendMessage(color("&eUsage: /timeline <past|present|find|portal>"));
return true;
}
World destinationWorld = args[0].equalsIgnoreCase("past") ? pastWorld : presentWorld;
player.teleport(destinationWorld.getSpawnLocation());
player.sendMessage(color(args[0].equalsIgnoreCase("past")
? "&bYou step into the world before the fall."
: "&7You return to the ruined Present."));
return true;
}
if (!sender.hasPermission("chronorelics.admin")) {
sender.sendMessage(color("&cYou do not have permission to give timeline relics."));
return true;
}
if (args.length == 2 && args[0].equalsIgnoreCase("timeslip")) {
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) {
sender.sendMessage(color("&cThat player is not online."));
return true;
}
beginTimeSlip(target);
return true;
}
if (args.length != 3 || !args[0].equalsIgnoreCase("give")) {
sender.sendMessage(color("&eUsage: /chronorelics give <player> <spear|mace|sword|elytra|axe|chronoglass|all> or /chronorelics timeslip <player>"));
return true;
}
Player target = Bukkit.getPlayerExact(args[1]);
if (target == null) {
sender.sendMessage(color("&cThat player is not online."));
return true;
}
String requested = args[2].toLowerCase();
if (requested.equals("all")) {
for (String relic : List.of("spear", "mace", "sword", "elytra", "axe")) {
giveRelic(target, relic);
}
sender.sendMessage(color("&aGave the five timeline relics to &f" + target.getName() + "&a."));
return true;
}
if (!List.of("spear", "mace", "sword", "elytra", "axe", "chronoglass").contains(requested)) {
sender.sendMessage(color("&cUnknown item. Choose spear, mace, sword, elytra, axe, chronoglass, or all."));
return true;
}
giveRelic(target, requested);
sender.sendMessage(color("&aGave the " + requested + " relic to &f" + target.getName() + "&a."));
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (command.getName().equalsIgnoreCase("timeline") && args.length == 1) {
return matching(List.of("past", "present", "find", "portal"), args[0]);
}
if (args.length == 1) {
return matching(List.of("give", "timeslip"), args[0]);
}
if (args.length == 2 && args[0].equalsIgnoreCase("timeslip")) {
return matching(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(), args[1]);
}
if (args.length == 2 && args[0].equalsIgnoreCase("give")) {
return matching(Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(), args[1]);
}
if (args.length == 3 && args[0].equalsIgnoreCase("give")) {
return matching(List.of("spear", "mace", "sword", "elytra", "axe", "chronoglass", "all"), args[2]);
}
return List.of();