forked from MassiveCraft/Vampire
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed NBTAPI, now using PersistentData Spigot API. That means minecraft updates won't break the plugin! Removed support for WorldGuard versions older than 7. Added the option to put all vampires inside of the permission group specified in the config file. Added HolyWater command. Added locales for Blood Flasks. Added "entry-vampires" worldguard flag that makes vampires unable to enter a region. With entry-vampires -g non_members deny you make that only vampires who have been invited to the region (members or owners) can go inside of it. Fixed Flasks not working properly. Fixed plugin doing weird stuff when you don't have worldguard installed. Extra: Added migrate.py to help migrating data from older versions (no DBHandler) to newer ones.
- Loading branch information
Showing
20 changed files
with
591 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,135 @@ | ||
package com.clanjhoo.vampire; | ||
|
||
import com.clanjhoo.vampire.entity.UPlayer; | ||
import com.clanjhoo.vampire.keyproviders.SkillMessageKeys; | ||
import com.clanjhoo.vampire.util.BooleanTagType; | ||
import com.clanjhoo.vampire.util.CollectionUtil; | ||
import org.bukkit.ChatColor; | ||
import com.clanjhoo.vampire.util.UUIDTagType; | ||
import org.bukkit.Material; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemFlag; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.PlayerInventory; | ||
import org.bukkit.inventory.meta.PotionMeta; | ||
import org.bukkit.persistence.PersistentDataContainer; | ||
import org.bukkit.persistence.PersistentDataType; | ||
import org.bukkit.potion.PotionEffect; | ||
import org.bukkit.potion.PotionEffectType; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.*; | ||
|
||
public class BloodFlaskUtil { | ||
private final static String COLOR_RED = ChatColor.RED.toString(); | ||
public final static String BLOOD_FLASK_NAME = ChatColor.GREEN.toString() + "Blood Flask"; | ||
public final static String BLOOD_FLASK_AMOUNT_SUFFIX = COLOR_RED + " units of blood."; | ||
public final static String BLOOD_FLASK_VAMPIRIC_TRUE = COLOR_RED + "The blood is vampiric."; | ||
public final static String BLOOD_FLASK_VAMPIRIC_FALSE = COLOR_RED + "The blood is not vampiric."; | ||
public final static PotionEffect BLOOD_FLASK_CUSTOM_EFFECT = new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20, 0); | ||
private final static NamespacedKey BLOOD_FLASK_KEY = new NamespacedKey(VampireRevamp.getInstance(), "flask"); | ||
private final static NamespacedKey BLOOD_FLASK_AMOUNT = new NamespacedKey(VampireRevamp.getInstance(), "amount"); | ||
private final static NamespacedKey BLOOD_FLASK_VAMPIRIC = new NamespacedKey(VampireRevamp.getInstance(), "vampiric"); | ||
private final static NamespacedKey BLOOD_FLASK_OWNER = new NamespacedKey(VampireRevamp.getInstance(), "owner"); | ||
|
||
public static ItemStack createBloodFlask(double amount, boolean isVampiric) { | ||
public static ItemStack createBloodFlask(Player creator, double amount, boolean isVampiric) { | ||
// Create a new item stack of material potion ... | ||
ItemStack ret = new ItemStack(Material.POTION); | ||
|
||
// ... and convert the isVampiric boolean into a string ... | ||
String metaVampiric = isVampiric ? BLOOD_FLASK_VAMPIRIC_TRUE : BLOOD_FLASK_VAMPIRIC_FALSE; | ||
SkillMessageKeys flaskVampKey = isVampiric ? SkillMessageKeys.FLASK_VAMPIRIC_TRUE : SkillMessageKeys.FLASK_VAMPIRIC_FALSE; | ||
String metaVampiric = VampireRevamp.getMessage(creator, flaskVampKey); | ||
|
||
// ... create the item lore ... | ||
List<String> lore = CollectionUtil.list( | ||
Double.toString(amount) + BLOOD_FLASK_AMOUNT_SUFFIX, | ||
VampireRevamp.getMessage(creator, SkillMessageKeys.FLASK_AMOUNT).replace("{amount}", Double.toString(amount)), | ||
metaVampiric | ||
); | ||
|
||
// ... and set the item meta ... | ||
PotionMeta meta = (PotionMeta) ret.getItemMeta(); | ||
meta.setDisplayName(BLOOD_FLASK_NAME); | ||
meta.setDisplayName(VampireRevamp.getMessage(creator, SkillMessageKeys.FLASK_NAME)); | ||
meta.setLore(lore); | ||
meta.addCustomEffect(BLOOD_FLASK_CUSTOM_EFFECT, false); | ||
meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_ATTRIBUTES); | ||
|
||
PersistentDataContainer flaskDC = meta.getPersistentDataContainer(); | ||
PersistentDataContainer flaskTag = flaskDC.getAdapterContext().newPersistentDataContainer(); | ||
flaskTag.set(BLOOD_FLASK_AMOUNT, PersistentDataType.DOUBLE, amount); | ||
flaskTag.set(BLOOD_FLASK_VAMPIRIC, BooleanTagType.TYPE, isVampiric); | ||
flaskTag.set(BLOOD_FLASK_OWNER, UUIDTagType.TYPE, creator.getUniqueId()); | ||
flaskDC.set(BLOOD_FLASK_KEY, PersistentDataType.TAG_CONTAINER, flaskTag); | ||
|
||
ret.setItemMeta(meta); | ||
|
||
// ... finally, return the result. | ||
return ret; | ||
} | ||
|
||
public static boolean isBloodFlaskVampiric(ItemStack item) { | ||
boolean success = false; | ||
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) { | ||
List<String> lore = item.getItemMeta().getLore(); | ||
if (lore != null && !lore.isEmpty() && lore.size() >= 2) { | ||
String stringBoolean = lore.get(1); | ||
success = BLOOD_FLASK_VAMPIRIC_TRUE.equals(stringBoolean); | ||
} | ||
} | ||
return success; | ||
} | ||
public static @Nullable BloodFlaskData getBloodFlaskData(@NotNull ItemStack item) { | ||
double amount; | ||
boolean isVampiric; | ||
UUID owner; | ||
|
||
public static double getBloodFlaskAmount(ItemStack item) { | ||
double amount = 0D; | ||
PersistentDataContainer flaskTag = item.getItemMeta().getPersistentDataContainer().get(BLOOD_FLASK_KEY, PersistentDataType.TAG_CONTAINER); | ||
|
||
if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()) { | ||
List<String> lore = item.getItemMeta().getLore(); | ||
if (lore != null && !lore.isEmpty()) { | ||
String amountLoreString = lore.get(0); | ||
String amountString[] = amountLoreString.split(" "); | ||
String amountStr = amountString[0].substring(0, 3); | ||
amount = Double.parseDouble(amountStr); | ||
} | ||
if (flaskTag == null) { | ||
return null; | ||
} | ||
|
||
return amount; | ||
} | ||
amount = flaskTag.get(BLOOD_FLASK_AMOUNT, PersistentDataType.DOUBLE); | ||
isVampiric = flaskTag.get(BLOOD_FLASK_VAMPIRIC, BooleanTagType.TYPE); | ||
owner = flaskTag.get(BLOOD_FLASK_OWNER, UUIDTagType.TYPE); | ||
|
||
public static boolean isBloodFlask(ItemStack item) { | ||
return item != null | ||
&& item.hasItemMeta() | ||
&& item.getItemMeta().hasDisplayName() | ||
&& item.getItemMeta().getDisplayName().equals(BLOOD_FLASK_NAME); | ||
return new BloodFlaskData(owner, amount, isVampiric); | ||
} | ||
|
||
public static void playerConsumeGlassBottle(@NotNull Player player) { | ||
private static boolean playerConsumeGlassBottle(@NotNull Player player) { | ||
ItemStack item = player.getInventory().getItemInMainHand(); | ||
if (item != null && item.getType() == Material.GLASS_BOTTLE) { | ||
|
||
if (item.getType() == Material.GLASS_BOTTLE) { | ||
int amount = item.getAmount(); | ||
if (amount > 1) { | ||
item.setAmount(amount - 1); | ||
} else { | ||
item = null; | ||
} | ||
player.getInventory().setItemInMainHand(item); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static void fillBottle(double amount, UPlayer uplayer) { | ||
public static boolean fillBottle(UPlayer uplayer, double amount) { | ||
Player player = uplayer.getPlayer(); | ||
BloodFlaskUtil.playerConsumeGlassBottle(player); | ||
if (!BloodFlaskUtil.playerConsumeGlassBottle(player)) { | ||
return false; | ||
} | ||
PlayerInventory playerInventory = player.getInventory(); | ||
playerInventory.addItem(BloodFlaskUtil.createBloodFlask(amount, uplayer.isVampire())); | ||
ItemStack flask = BloodFlaskUtil.createBloodFlask(player, amount, uplayer.isVampire()); | ||
Map<Integer, ItemStack> result = playerInventory.addItem(flask); | ||
if (result.size() > 0) { | ||
player.getWorld().dropItem(player.getLocation(), flask); | ||
} | ||
return true; | ||
} | ||
|
||
public static class BloodFlaskData { | ||
private final UUID owner; | ||
private final double amount; | ||
private final boolean isVampiric; | ||
|
||
public BloodFlaskData(UUID owner, double amount, boolean isVampiric) { | ||
this.owner = owner; | ||
this.amount = amount; | ||
this.isVampiric = isVampiric; | ||
} | ||
|
||
public UUID getOwner() { | ||
return owner; | ||
} | ||
|
||
public double getAmount() { | ||
return amount; | ||
} | ||
|
||
public boolean isVampiric() { | ||
return isVampiric; | ||
} | ||
} | ||
} |
Oops, something went wrong.