Files
src
build.gradle
settings.gradle
Code
8 523
src/main/java/com/manepear/ManePearPlugin.java
package com.manepear;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.NamespacedKey;
import org.bukkit.potion.PotionEffect;
import org.bukkit.plugin.java.JavaPlugin;

public final class ManePearPlugin extends JavaPlugin {
    private final Map<World, Boolean> daylightCycleStates = new HashMap<>();
    private final Map<World, Map<UUID, PotionEffect>> madeInHeavenEffects = new HashMap<>();
    private NamespacedKey clockKey;
    private SpeedGlitchManager speedGlitchManager;
    private WalkSpeedAccelerationManager walkSpeedAccelerationManager;

    @Override
    public void onEnable() {
        clockKey = new NamespacedKey(this, "made_in_heaven_clock");
        speedGlitchManager = new SpeedGlitchManager(this);
        walkSpeedAccelerationManager = new WalkSpeedAccelerationManager();

        ManePearCommand command = new ManePearCommand(this);
        getCommand("walkspeed").setExecutor(command);
        getCommand("speedglitch").setExecutor(command);
        getCommand("madeinheaven").setExecutor(command);
        getServer().getPluginManager().registerEvents(new ManePearListener(this), this);
        Bukkit.getScheduler().runTaskTimer(this, walkSpeedAccelerationManager::tick, 1L, 1L);

        Bukkit.getScheduler().runTaskTimer(this, () -> {
            for (World world : daylightCycleStates.keySet()) {
                world.setFullTime(world.getFullTime() + 200);
            }
        }, 1L, 1L);
    }

    @Override
    public void onDisable() {
        if (speedGlitchManager != null) {
            speedGlitchManager.shutdown();
        }
        if (walkSpeedAccelerationManager != null) {
            walkSpeedAccelerationManager.shutdown();
        }
        daylightCycleStates.forEach((world, wasEnabled) ->
                world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, wasEnabled));
    }

    public SpeedGlitchManager getSpeedGlitchManager() {
        return speedGlitchManager;
    }

    public WalkSpeedAccelerationManager getWalkSpeedAccelerationManager() {
        return walkSpeedAccelerationManager;
    }

    public ItemStack createMadeInHeavenClock() {
        ItemStack clock = XMaterial.matchXMaterial("CLOCK").map(XMaterial::parseItem).orElse(null);
        if (clock == null) {
            return null;
        }
        ItemMeta meta = clock.getItemMeta();
        if (meta == null) {
            return null;
        }
        meta.setDisplayName("§dMade in Heaven");
        meta.setLore(java.util.List.of("§7Right-click to accelerate time."));
        meta.getPersistentDataContainer().set(clockKey, PersistentDataType.BYTE, (byte) 1);
        clock.setItemMeta(meta);
        return clock;
    }

    public boolean isMadeInHeavenClock(ItemStack item) {
        if (item == null || !item.hasItemMeta()) {
            return false;
        }
        return item.getItemMeta().getPersistentDataContainer().has(clockKey, PersistentDataType.BYTE);
    }

    public void activateMadeInHeaven(Player player) {
        World world = player.getWorld();
        XPotion.matchXPotion("SPEED")
                .map(potion -> potion.buildPotionEffect(Integer.MAX_VALUE, 8))
                .ifPresent(effect -> {
                    player.addPotionEffect(effect);
                    madeInHeavenEffects.computeIfAbsent(world, key -> new HashMap<>())
                            .put(player.getUniqueId(), effect);
                });

        if (!daylightCycleStates.containsKey(world)) {
            daylightCycleStates.put(world, Boolean.TRUE.equals(world.getGameRuleValue(GameRule.DO_DAYLIGHT_CYCLE)));
            world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
        }
        player.sendMessage("§dMade in Heaven has begun.");
    }

    public void stopMadeInHeaven(Player player) {
        World world = player.getWorld();
        boolean wasActive = daylightCycleStates.containsKey(world) || madeInHeavenEffects.containsKey(world);
        Boolean wasDaylightCycleEnabled = daylightCycleStates.remove(world);
        if (wasDaylightCycleEnabled != null) {
            world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, wasDaylightCycleEnabled);
        }

        Map<UUID, PotionEffect> effects = madeInHeavenEffects.remove(world);
        if (effects != null) {
            for (Map.Entry<UUID, PotionEffect> entry : effects.entrySet()) {
                Player affected = Bukkit.getPlayer(entry.getKey());
                if (affected != null) {
                    affected.removePotionEffect(entry.getValue().getType());
                }
            }
        }

        player.sendMessage(wasActive ? "§dMade in Heaven stopped."
                : "§cMade in Heaven is not active in this world.");
    }
}