Files
src
build.gradle
settings.gradle
Code
4 2,815
src/main/java/com/example/stormgems/GemPlugin.java
package com.example.stormgems;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.ArmorStand;
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.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
public class GemPlugin extends JavaPlugin implements Listener, CommandExecutor {
/*
* ============================================================
* SETTINGS
* ============================================================
*/
private static final int MAX_INTEGRITY = 100;
// Every ability use costs exactly 1 integrity.
private static final int ABILITY_INTEGRITY_COST = 1;
// Player health:
// Bukkit uses 20.0 = 10 hearts.
// 200.0 = 100 hearts.
private static final double PLAYER_MAX_HEALTH = 200.0;
// How much a Gem Shard repairs.
private static final int SHARD_REPAIR_AMOUNT = 10;
/*
* ============================================================
* DATA
* ============================================================
*/
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
private final Map<UUID, Location> anchors = new HashMap<>();
private final Map<UUID, Long> anchorTimes = new HashMap<>();
/*
* This controls whether new players automatically receive
* a random gem.
*/
private boolean starterGemsEnabled = false;
/*
* Used to make sure a player receives a starter gem only once.
*/
private final Map<UUID, Boolean> receivedStarterGem = new HashMap<>();
/*
* Very slow gem wear from attacking.
*
* Instead of losing 1 integrity every hit, a player gets
* 1 integrity damage every 10 attacks.
*/
private final Map<UUID, Integer> attackCounters = new HashMap<>();
/*
* ============================================================
* GEM NAMES
* ============================================================
*/
private final Map<String, String> gemNames = Map.of(
"storm", "§bStorm Gem",
"magma", "§cMagma Gem",
"frost", "§fFrost Gem",
"shadow", "§5Shadow Gem",
"nature", "§aNature Gem",
"tide", "§3Tide Gem",
"void", "§8Void Gem",
"gilded", "§6Gilded Gem"
);
private NamespacedKey gemKey;
private NamespacedKey integrityKey;
private NamespacedKey shardKey;
/*
* ============================================================
* ENABLE
* ============================================================
*/
@Override
public void onEnable() {
gemKey = new NamespacedKey(this, "gem_type");
integrityKey = new NamespacedKey(this, "integrity");
shardKey = new NamespacedKey(this, "gem_shard");
getServer().getPluginManager().registerEvents(this, this);
PluginCommand command = getCommand("gem");
if (command != null) {
command.setExecutor(this);
}
getLogger().info("Storm Gems enabled!");
}
/*
* ============================================================
* XSERIES HELPERS
* ============================================================
*/
private Material material(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() ->
new IllegalArgumentException(
"Unsupported material: " + name));
}
private String blockName(Block block) {
return XMaterial.matchXMaterial(block.getType()).name();
}
private void playSound(Player player, String name) {
XSound.matchXSound(name)
.ifPresent(sound -> sound.play(player));
}
private void effect(
LivingEntity entity,
String name,
int duration,
int amplifier) {
XPotion.matchXPotion(name)
.map(potion ->
potion.buildPotionEffect(duration, amplifier))
.ifPresent(entity::addPotionEffect);
}
private void effect(
LivingEntity entity,
String name,
int duration,
int amplifier,
boolean ambient,
boolean particles) {
XPotion.matchXPotion(name)
.map(potion -> {
PotionEffect base =
potion.buildPotionEffect(duration, amplifier);
return new PotionEffect(
base.getType(),