forked from SteamWar/SteamWar
Format code
This commit is contained in:
@@ -64,7 +64,9 @@ public class CustomItem extends SpecialItem {
|
||||
|
||||
public static void init() {
|
||||
File itemsFolder = new File(MissileWars.getPlugin().getDataFolder(), "items");
|
||||
if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) throw new SecurityException("Items could not be loaded");
|
||||
if (!itemsFolder.exists() || !itemsFolder.canRead() || !itemsFolder.isDirectory()) {
|
||||
throw new SecurityException("Items could not be loaded");
|
||||
}
|
||||
for (File itemFile : Objects.requireNonNull(itemsFolder.listFiles())) {
|
||||
if (!itemFile.canRead() || !itemFile.isFile()) continue;
|
||||
try {
|
||||
|
||||
@@ -48,88 +48,90 @@ import java.util.Objects;
|
||||
|
||||
public class Missile extends SpecialItem {
|
||||
|
||||
private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0));
|
||||
private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0));
|
||||
|
||||
private final Clipboard clipboard;
|
||||
private final ItemStack item;
|
||||
private final Clipboard clipboard;
|
||||
private final ItemStack item;
|
||||
|
||||
private Missile(File missileFile) {
|
||||
String[] strings = missileFile.getName().split("\\.");
|
||||
String name = strings[0];
|
||||
String material = strings[1];
|
||||
if (!material.endsWith("_SPAWN_EGG")) material += "_SPAWN_EGG";
|
||||
Material itemType = Material.valueOf(material);
|
||||
private Missile(File missileFile) {
|
||||
String[] strings = missileFile.getName().split("\\.");
|
||||
String name = strings[0];
|
||||
String material = strings[1];
|
||||
if (!material.endsWith("_SPAWN_EGG")) material += "_SPAWN_EGG";
|
||||
Material itemType = Material.valueOf(material);
|
||||
|
||||
ClipboardFormat format = ClipboardFormats.findByFile(missileFile);
|
||||
try {
|
||||
assert format != null;
|
||||
clipboard = format.getReader(new FileInputStream(missileFile)).read();
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Corrupt missile");
|
||||
}
|
||||
ClipboardFormat format = ClipboardFormats.findByFile(missileFile);
|
||||
try {
|
||||
assert format != null;
|
||||
clipboard = format.getReader(new FileInputStream(missileFile)).read();
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException("Corrupt missile");
|
||||
}
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore(lore, strings, 2, "§7Speed");
|
||||
lore(lore, strings, 3, "§7Size");
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore(lore, strings, 2, "§7Speed");
|
||||
lore(lore, strings, 3, "§7Size");
|
||||
|
||||
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
|
||||
BlockTypeMask blockTypeMask = new BlockTypeMask(clipboard, BlockTypes.TNT);
|
||||
lore.add("§7TNT §8: §e" + e.countBlocks(clipboard.getRegion(), blockTypeMask));
|
||||
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
|
||||
BlockTypeMask blockTypeMask = new BlockTypeMask(clipboard, BlockTypes.TNT);
|
||||
lore.add("§7TNT §8: §e" + e.countBlocks(clipboard.getRegion(), blockTypeMask));
|
||||
|
||||
item = createItem(itemType, "§c" + name, 1, lore);
|
||||
}
|
||||
item = createItem(itemType, "§c" + name, 1, lore);
|
||||
}
|
||||
|
||||
private void lore(List<String> lore, String[] args, int index, String tag) {
|
||||
if (args.length > index) {
|
||||
lore.add(tag + " §8: §e" + Integer.parseInt(args[index]) + "§8/§77");
|
||||
} else {
|
||||
lore.add(tag + " §8: §e0§8/§77");
|
||||
}
|
||||
}
|
||||
private void lore(List<String> lore, String[] args, int index, String tag) {
|
||||
if (args.length > index) {
|
||||
lore.add(tag + " §8: §e" + Integer.parseInt(args[index]) + "§8/§77");
|
||||
} else {
|
||||
lore.add(tag + " §8: §e0§8/§77");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
@Override
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleUse(Player p) {
|
||||
BlockVector3 dimensions = clipboard.getDimensions();
|
||||
Location location = p.getLocation();
|
||||
BlockVector3 v = BlockVector3.ZERO;
|
||||
BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
|
||||
AffineTransform aT = new AffineTransform();
|
||||
@Override
|
||||
public boolean handleUse(Player p) {
|
||||
BlockVector3 dimensions = clipboard.getDimensions();
|
||||
Location location = p.getLocation();
|
||||
BlockVector3 v = BlockVector3.ZERO;
|
||||
BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
|
||||
AffineTransform aT = new AffineTransform();
|
||||
|
||||
double yaw = (p.getLocation().getYaw() + 360f) % 360;
|
||||
if (yaw > 45 && yaw <= 135) aT = aT.rotateY(270);
|
||||
else if (yaw > 135 && yaw <= 225) aT = aT.rotateY(180);
|
||||
else if (yaw > 225 && yaw <= 315) aT = aT.rotateY(90);
|
||||
double yaw = (p.getLocation().getYaw() + 360f) % 360;
|
||||
if (yaw > 45 && yaw <= 135) aT = aT.rotateY(270);
|
||||
else if (yaw > 135 && yaw <= 225) aT = aT.rotateY(180);
|
||||
else if (yaw > 225 && yaw <= 315) aT = aT.rotateY(90);
|
||||
|
||||
v = v.subtract(dimensions.x()/2, dimensions.y() + 2, -2).subtract(offset);
|
||||
v = aT.apply(v.toVector3()).toBlockPoint();
|
||||
v = v.add(location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
||||
v = v.subtract(dimensions.x() / 2, dimensions.y() + 2, -2).subtract(offset);
|
||||
v = aT.apply(v.toVector3()).toBlockPoint();
|
||||
v = v.add(location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
||||
|
||||
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
|
||||
ClipboardHolder ch = new ClipboardHolder(clipboard);
|
||||
ch.setTransform(aT);
|
||||
Operations.completeBlindly(ch.createPaste(e).to(v).ignoreAirBlocks(true).build());
|
||||
e.flushSession();
|
||||
return true;
|
||||
}
|
||||
EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
|
||||
ClipboardHolder ch = new ClipboardHolder(clipboard);
|
||||
ch.setTransform(aT);
|
||||
Operations.completeBlindly(ch.createPaste(e).to(v).ignoreAirBlocks(true).build());
|
||||
e.flushSession();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMissile() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean isMissile() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles");
|
||||
if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) throw new SecurityException("Missiles could not be loaded");
|
||||
for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) {
|
||||
if (!missileFile.canRead() || !missileFile.isFile()) continue;
|
||||
if (!missileFile.getName().endsWith(".schem")) continue;
|
||||
new Missile(missileFile);
|
||||
}
|
||||
}
|
||||
public static void init() {
|
||||
File missileFolder = new File(MissileWars.getPlugin().getDataFolder(), "missiles");
|
||||
if (!missileFolder.exists() || !missileFolder.canRead() || !missileFolder.isDirectory()) {
|
||||
throw new SecurityException("Missiles could not be loaded");
|
||||
}
|
||||
for (File missileFile : Objects.requireNonNull(missileFolder.listFiles())) {
|
||||
if (!missileFile.canRead() || !missileFile.isFile()) continue;
|
||||
if (!missileFile.getName().endsWith(".schem")) continue;
|
||||
new Missile(missileFile);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,94 +39,103 @@ import java.util.Random;
|
||||
|
||||
public abstract class SpecialItem {
|
||||
|
||||
private static final Random random = new Random();
|
||||
private static double count = 0;
|
||||
private static int consecutiveSupportItems = 0;
|
||||
private static final Random random = new Random();
|
||||
private static double count = 0;
|
||||
private static int consecutiveSupportItems = 0;
|
||||
|
||||
private static List<SpecialItem> supportItems = new ArrayList<>();
|
||||
private static List<SpecialItem> missileItems = new ArrayList<>();
|
||||
private static List<SpecialItem> supportItems = new ArrayList<>();
|
||||
private static List<SpecialItem> missileItems = new ArrayList<>();
|
||||
|
||||
SpecialItem() {
|
||||
if (this.isMissile()) missileItems.add(this);
|
||||
else supportItems.add(this);
|
||||
}
|
||||
SpecialItem() {
|
||||
if (this.isMissile()) missileItems.add(this);
|
||||
else supportItems.add(this);
|
||||
}
|
||||
|
||||
private String materialName = null;
|
||||
public abstract ItemStack getItem();
|
||||
public abstract boolean handleUse(Player p);
|
||||
public void handleThrow(Entity e) {}
|
||||
public void handleHit(Entity e, Location l) {}
|
||||
public boolean isMissile() {
|
||||
return false;
|
||||
}
|
||||
private String materialName = null;
|
||||
|
||||
public ItemStack createItem(Material material, String name, int amount, List<String> lore) {
|
||||
ItemStack item = new ItemStack(material, amount);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
assert meta != null;
|
||||
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
||||
meta.setLore(lore);
|
||||
meta.setDisplayName(name);
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
public abstract ItemStack getItem();
|
||||
|
||||
public static boolean handleUse(ItemStack item, Player player) {
|
||||
if (player.hasPotionEffect(PotionEffectType.LEVITATION)) {
|
||||
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cDu kannst dieses Item derzeit nicht nutzen!"));
|
||||
return false;
|
||||
}
|
||||
return handleUse(item, player, missileItems) || handleUse(item, player, supportItems);
|
||||
}
|
||||
public abstract boolean handleUse(Player p);
|
||||
|
||||
private static boolean handleUse(ItemStack item, Player player, List<SpecialItem> items) {
|
||||
for (SpecialItem specialItem : items)
|
||||
if (item.isSimilar(specialItem.getItem())) return specialItem.handleUse(player);
|
||||
return false;
|
||||
}
|
||||
public void handleThrow(Entity e) {
|
||||
}
|
||||
|
||||
public static void handleThrow(ProjectileLaunchEvent e) {
|
||||
String name = e.getEntity().getClass().getName().toLowerCase();
|
||||
for (SpecialItem specialItem : supportItems) {
|
||||
if (specialItem.materialName == null)
|
||||
specialItem.materialName = specialItem.getItem().getType().name().toLowerCase();
|
||||
if (name.contains(specialItem.materialName))
|
||||
specialItem.handleThrow(e.getEntity());
|
||||
}
|
||||
}
|
||||
public void handleHit(Entity e, Location l) {
|
||||
}
|
||||
|
||||
public static void handleHit(ProjectileHitEvent e) {
|
||||
String name = e.getEntity().getClass().getName().toLowerCase();
|
||||
public boolean isMissile() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Location location = null;
|
||||
if (e.getHitEntity() != null) location = e.getHitEntity().getLocation();
|
||||
else if (e.getHitBlock() != null) location = e.getHitBlock().getLocation();
|
||||
if (location == null) return;
|
||||
public ItemStack createItem(Material material, String name, int amount, List<String> lore) {
|
||||
ItemStack item = new ItemStack(material, amount);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
assert meta != null;
|
||||
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
||||
meta.setLore(lore);
|
||||
meta.setDisplayName(name);
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
for (SpecialItem specialItem : supportItems) {
|
||||
if (name.contains(((CustomItem) specialItem).getScriptedItem().getEntityName())) {
|
||||
specialItem.handleHit(e.getEntity(), location);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static boolean handleUse(ItemStack item, Player player) {
|
||||
if (player.hasPotionEffect(PotionEffectType.LEVITATION)) {
|
||||
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§cDu kannst dieses Item derzeit nicht nutzen!"));
|
||||
return false;
|
||||
}
|
||||
return handleUse(item, player, missileItems) || handleUse(item, player, supportItems);
|
||||
}
|
||||
|
||||
public static ItemStack getRandomItem() {
|
||||
if (Config.MissileChance == 0) {
|
||||
return supportItems.get(random.nextInt(supportItems.size())).getItem();
|
||||
}
|
||||
if (Config.MissileChance == 1 || consecutiveSupportItems > 1) {
|
||||
consecutiveSupportItems = 0;
|
||||
return missileItems.get(random.nextInt(missileItems.size())).getItem();
|
||||
}
|
||||
private static boolean handleUse(ItemStack item, Player player, List<SpecialItem> items) {
|
||||
for (SpecialItem specialItem : items)
|
||||
if (item.isSimilar(specialItem.getItem())) return specialItem.handleUse(player);
|
||||
return false;
|
||||
}
|
||||
|
||||
double missileChance = Config.MissileChance + count * 0.1;
|
||||
if (random.nextDouble() > missileChance) {
|
||||
count += Config.MissileChance;
|
||||
consecutiveSupportItems++;
|
||||
return supportItems.get(random.nextInt(supportItems.size())).getItem();
|
||||
} else {
|
||||
count -= 1 - Config.MissileChance;
|
||||
return missileItems.get(random.nextInt(missileItems.size())).getItem();
|
||||
}
|
||||
}
|
||||
public static void handleThrow(ProjectileLaunchEvent e) {
|
||||
String name = e.getEntity().getClass().getName().toLowerCase();
|
||||
for (SpecialItem specialItem : supportItems) {
|
||||
if (specialItem.materialName == null) {
|
||||
specialItem.materialName = specialItem.getItem().getType().name().toLowerCase();
|
||||
}
|
||||
if (name.contains(specialItem.materialName)) {
|
||||
specialItem.handleThrow(e.getEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleHit(ProjectileHitEvent e) {
|
||||
String name = e.getEntity().getClass().getName().toLowerCase();
|
||||
|
||||
Location location = null;
|
||||
if (e.getHitEntity() != null) location = e.getHitEntity().getLocation();
|
||||
else if (e.getHitBlock() != null) location = e.getHitBlock().getLocation();
|
||||
if (location == null) return;
|
||||
|
||||
for (SpecialItem specialItem : supportItems) {
|
||||
if (name.contains(((CustomItem) specialItem).getScriptedItem().getEntityName())) {
|
||||
specialItem.handleHit(e.getEntity(), location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack getRandomItem() {
|
||||
if (Config.MissileChance == 0) {
|
||||
return supportItems.get(random.nextInt(supportItems.size())).getItem();
|
||||
}
|
||||
if (Config.MissileChance == 1 || consecutiveSupportItems > 1) {
|
||||
consecutiveSupportItems = 0;
|
||||
return missileItems.get(random.nextInt(missileItems.size())).getItem();
|
||||
}
|
||||
|
||||
double missileChance = Config.MissileChance + count * 0.1;
|
||||
if (random.nextDouble() > missileChance) {
|
||||
count += Config.MissileChance;
|
||||
consecutiveSupportItems++;
|
||||
return supportItems.get(random.nextInt(supportItems.size())).getItem();
|
||||
} else {
|
||||
count -= 1 - Config.MissileChance;
|
||||
return missileItems.get(random.nextInt(missileItems.size())).getItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user