Files
src
build.gradle
settings.gradle
Code
5 487
src/main/java/com/kodari/starwars/StarWarsAddon.java
package com.kodari.starwars;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowball;
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.ProjectileHitEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
import org.bukkit.plugin.java.JavaPlugin;
public final class StarWarsAddon extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private static final String PREFIX = "&8[&bStar Wars&8] &f";
private static final String CRYSTAL_ID = "cyber_crystal";
private final Map<UUID, Long> gunCooldowns = new HashMap<>();
private NamespacedKey itemKey;
private NamespacedKey weaponKey;
private NamespacedKey levelKey;
private NamespacedKey colorKey;
private NamespacedKey projectileKey;
private double crystalDropChance;
private int minimumCrystalDrop;
private int maximumCrystalDrop;
private int maximumUpgrade;
private int gunCooldownTicks;
@Override
public void onEnable() {
itemKey = new NamespacedKey(this, "item");
weaponKey = new NamespacedKey(this, "weapon");
levelKey = new NamespacedKey(this, "upgrade_level");
colorKey = new NamespacedKey(this, "crystal_color");
projectileKey = new NamespacedKey(this, "projectile_weapon");
saveDefaultConfig();
crystalDropChance = getConfig().getDouble("cyber-crystal.mining-drop-chance", 0.25D);
minimumCrystalDrop = getConfig().getInt("cyber-crystal.minimum-drop", 1);
maximumCrystalDrop = getConfig().getInt("cyber-crystal.maximum-drop", 2);
maximumUpgrade = getConfig().getInt("weapons.maximum-upgrade", 5);
gunCooldownTicks = getConfig().getInt("weapons.gun-cooldown-ticks", 10);
getServer().getPluginManager().registerEvents(this, this);
getCommand("starwars").setExecutor(this);
getCommand("starwars").setTabCompleter(this);
registerRecipes();
}
@Override
public void onDisable() {
gunCooldowns.clear();
}
private void registerRecipes() {
Material quartz = material("QUARTZ");
Material ironNugget = material("IRON_NUGGET");
Material amethyst = material("AMETHYST_SHARD");
Material diamond = material("DIAMOND");
Material iron = material("IRON_INGOT");
Material redstone = material("REDSTONE");
Material stick = material("STICK");
ShapedRecipe crystalRecipe = new ShapedRecipe(new NamespacedKey(this, CRYSTAL_ID), createCyberCrystal());
crystalRecipe.shape("QQQ", "QIQ", "QQQ");
crystalRecipe.setIngredient('Q', exact(quartz));
crystalRecipe.setIngredient('I', exact(ironNugget));
getServer().addRecipe(crystalRecipe);
for (WeaponType type : WeaponType.values()) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, type.id), createWeapon(type, 0));
if (type.isLightsaber) {
recipe.shape(" C ", " D ", " I ");
recipe.setIngredient('C', new RecipeChoice.ExactChoice(createCyberCrystal(type.crystalColor)));
recipe.setIngredient('D', exact(diamond));
recipe.setIngredient('I', exact(iron));
} else if (type == WeaponType.DL44) {
recipe.shape("IRC", " II", " SI");
recipe.setIngredient('I', exact(iron));
recipe.setIngredient('R', exact(redstone));
recipe.setIngredient('C', new RecipeChoice.ExactChoice(createCyberCrystal("amber")));
recipe.setIngredient('S', exact(stick));
} else if (type == WeaponType.E11) {
recipe.shape(" II", "IRC", " SI");
recipe.setIngredient('I', exact(iron));
recipe.setIngredient('R', exact(redstone));
recipe.setIngredient('C', new RecipeChoice.ExactChoice(createCyberCrystal("blue")));
recipe.setIngredient('S', exact(stick));
} else {
recipe.shape(" II", "ICR", " SI");
recipe.setIngredient('I', exact(iron));
recipe.setIngredient('R', exact(redstone));
recipe.setIngredient('C', new RecipeChoice.ExactChoice(createCyberCrystal("green")));
recipe.setIngredient('S', exact(stick));
}
getServer().addRecipe(recipe);
}
}
private RecipeChoice.ExactChoice exact(Material material) {
return new RecipeChoice.ExactChoice(new ItemStack(material));
}
@EventHandler
public void onCyberCrystalOreBreak(BlockBreakEvent event) {
String blockType = event.getBlock().getType().name();
if (!blockType.equals("DIAMOND_ORE") && !blockType.equals("DEEPSLATE_DIAMOND_ORE")) {
return;
}
if (Math.random() > crystalDropChance) {
return;
}
int amount = minimumCrystalDrop;
if (maximumCrystalDrop > minimumCrystalDrop) {
amount += (int) (Math.random() * (maximumCrystalDrop - minimumCrystalDrop + 1));
}
ItemStack drop = createCyberCrystal();
drop.setAmount(amount);
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), drop);
}
@EventHandler
public void onWeaponUse(PlayerInteractEvent event) {
if (event.getItem() == null || !isWeapon(event.getItem())) {
return;
}
if (!event.getAction().name().startsWith("RIGHT_CLICK")) {
return;
}
WeaponType type = getWeaponType(event.getItem());
if (type == null) {
return;
}
event.setCancelled(true);
if (type.isLightsaber) {
useForcePush(event.getPlayer(), type);
} else {
fireBlaster(event.getPlayer(), type);
}
}
private void useForcePush(Player player, WeaponType type) {
World world = player.getWorld();
for (Entity entity : player.getNearbyEntities(7.0D, 4.0D, 7.0D)) {
if (!(entity instanceof LivingEntity target) || entity.equals(player)) {
continue;
}
Vector push = target.getLocation().toVector().subtract(player.getLocation().toVector());
if (push.lengthSquared() == 0.0D) {
continue;
}
target.setVelocity(push.normalize().multiply(1.35D).setY(0.6D));
target.damage(2.0D, player);
if (type == WeaponType.RED_SABER) {
target.setFireTicks(80);
}
}
player.sendMessage(color(PREFIX + type.displayName + " &7Force Push unleashed."));
world.playSound(player.getLocation(), "entity.generic.explode", 0.45F, 1.8F);
}
private void fireBlaster(Player player, WeaponType type) {
long now = System.currentTimeMillis();
long cooldown = gunCooldownTicks * 50L;
Long lastShot = gunCooldowns.get(player.getUniqueId());
if (lastShot != null && now - lastShot < cooldown) {
return;
}
gunCooldowns.put(player.getUniqueId(), now);
Snowball projectile = player.launchProjectile(Snowball.class);
PersistentDataContainer data = projectile.getPersistentDataContainer();