forked from SteamWar/SteamWar
Add MissileWars module
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
This file is a part of the SteamWar software.
|
||||
|
||||
Copyright (C) 2020 SteamWar.de-Serverteam
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.misslewars.items;
|
||||
|
||||
import de.steamwar.misslewars.Config;
|
||||
import net.md_5.bungee.api.ChatMessageType;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.ProjectileHitEvent;
|
||||
import org.bukkit.event.entity.ProjectileLaunchEvent;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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 List<SpecialItem> supportItems = new ArrayList<>();
|
||||
private static List<SpecialItem> missileItems = new ArrayList<>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
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 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 || consecutiveSupportItems > 1) {
|
||||
consecutiveSupportItems = 0;
|
||||
return supportItems.get(random.nextInt(supportItems.size())).getItem();
|
||||
}
|
||||
if (Config.MissileChance == 1) {
|
||||
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