Small Update

Updated Vault Support
Items can now grant permissions when held
This commit is contained in:
ASangarin 2020-10-16 18:29:38 +02:00
parent 402daf6cbb
commit 6c2cca379e
6 changed files with 187 additions and 17 deletions

View File

@ -120,6 +120,7 @@ public class MMOItems extends JavaPlugin {
private PlayerInventory inventory = new DefaultPlayerInventory();
private FlagPlugin flagPlugin = new DefaultFlags();
private HologramSupport hologramSupport;
private VaultSupport vaultSupport;
private RPGHandler rpgPlugin;
public void onLoad() {
@ -190,10 +191,8 @@ public class MMOItems extends JavaPlugin {
worldGenManager = new WorldGenManager();
blockManager = new BlockManager();
if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
new VaultSupport();
getLogger().log(Level.INFO, "Hooked onto Vault");
}
if (Bukkit.getPluginManager().getPlugin("Vault") != null)
vaultSupport = new VaultSupport();
getLogger().log(Level.INFO, "Loading crafting stations, please wait..");
layoutManager.reload();
@ -217,7 +216,7 @@ public class MMOItems extends JavaPlugin {
* effects and this class will be registered as a listener. starts with
* a 5s delay to let the other plugins time to load nicely
*/
Bukkit.getScheduler().runTaskTimer(this, () -> Bukkit.getOnlinePlayers().forEach(player -> PlayerData.get(player).updateEffects()), 100, 20);
Bukkit.getScheduler().runTaskTimer(this, () -> Bukkit.getOnlinePlayers().forEach(player -> PlayerData.get(player).updateStats()), 100, 20);
/*
* this tasks updates twice a second player inventories on the server.
@ -486,6 +485,17 @@ public class MMOItems extends JavaPlugin {
return itemManager;
}
/*
* External API's
*/
public boolean hasVault() {
return vaultSupport != null;
}
public VaultSupport getVault() {
return vaultSupport;
}
public List<StringInputParser> getStringInputParsers() {
return stringInputParsers;
}

View File

@ -41,7 +41,9 @@ import net.Indyuce.mmoitems.stat.data.AbilityData;
import net.Indyuce.mmoitems.stat.data.AbilityListData;
import net.Indyuce.mmoitems.stat.data.ParticleData;
import net.Indyuce.mmoitems.stat.data.PotionEffectListData;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.milkbowl.vault.permission.Permission;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.DamageType;
import net.mmogroup.mmolib.api.item.NBTItem;
@ -78,6 +80,7 @@ public class PlayerData {
private Set<ParticleRunnable> itemParticles = new HashSet<>();
private ParticleRunnable overridingItemParticles = null;
private Set<AbilityData> itemAbilities = new HashSet<>();
private Set<String> permissions = new HashSet<>();
private boolean fullHands = false;
private SetBonuses setBonuses = null;
private final PlayerStats stats;
@ -99,6 +102,13 @@ public class PlayerData {
}
public void save() {
if(MMOItems.plugin.hasVault()) {
Permission perms = MMOItems.plugin.getVault().getPermissions();
permissions.forEach(perm -> {
if(perms.has(getPlayer(), perm))
perms.playerRemove(getPlayer(), perm);
});
}
cancelRunnables();
ConfigFile config = new ConfigFile("/userdata", getUniqueId().toString());
@ -182,6 +192,14 @@ public class PlayerData {
cancelRunnables();
itemParticles.clear();
overridingItemParticles = null;
if(MMOItems.plugin.hasVault()) {
Permission perms = MMOItems.plugin.getVault().getPermissions();
permissions.forEach(perm -> {
if(perms.has(getPlayer(), perm))
perms.playerRemove(getPlayer(), perm);
});
}
permissions.clear();
/*
* updates the full-hands boolean, this way it can be cached and used in
@ -248,6 +266,12 @@ public class PlayerData {
} else
((AbilityListData) item.getData(ItemStat.ABILITIES)).getAbilities().forEach(ability -> itemAbilities.add(ability));
}
/*
* apply permissions if vault exists
*/
if (MMOItems.plugin.hasVault() && item.hasData(ItemStat.GRANTED_PERMISSIONS))
permissions.addAll(((StringListData) item.getData(ItemStat.GRANTED_PERMISSIONS)).getList());
}
/*
@ -303,7 +327,7 @@ public class PlayerData {
offhand = getPlayer().getInventory().getItemInOffHand();
}
public void updateEffects() {
public void updateStats() {
if(!mmoData.isOnline()) return;
// perm effects
@ -317,6 +341,15 @@ public class PlayerData {
getPlayer().removePotionEffect(PotionEffectType.SLOW);
getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 40, 1, true, false));
}
// permissions
if(MMOItems.plugin.hasVault()) {
Permission perms = MMOItems.plugin.getVault().getPermissions();
permissions.forEach(perm -> {
if(!perms.has(getPlayer(), perm))
perms.playerAdd(getPlayer(), perm);
});
}
}
public SetBonuses getSetBonuses() {

View File

@ -1,26 +1,24 @@
package net.Indyuce.mmoitems.comp.eco;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.crafting.condition.Condition;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.milkbowl.vault.economy.Economy;
import net.mmogroup.mmolib.api.MMOLineConfig;
public class MoneyCondition extends Condition {
private final double amount;
private final Economy economy;
public MoneyCondition(Economy economy, MMOLineConfig config) {
public MoneyCondition(MMOLineConfig config) {
super("money");
config.validate("amount");
amount = config.getDouble("amount");
this.economy = economy;
}
@Override
public boolean isMet(PlayerData data) {
if(!data.isOnline()) return false;
return economy.has(data.getPlayer(), amount);
return MMOItems.plugin.getVault().getEconomy().has(data.getPlayer(), amount);
}
@Override
@ -31,6 +29,6 @@ public class MoneyCondition extends Condition {
@Override
public void whenCrafting(PlayerData data) {
if(!data.isOnline()) return;
economy.withdrawPlayer(data.getPlayer(), amount);
MMOItems.plugin.getVault().getEconomy().withdrawPlayer(data.getPlayer(), amount);
}
}

View File

@ -8,24 +8,52 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.crafting.ConditionalDisplay;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import net.mmogroup.mmolib.api.util.AltChar;
public class VaultSupport {
private final Economy economy;
private final Permission permissions;
private final boolean enabled;
public VaultSupport() {
RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServer().getServicesManager()
.getRegistration(Economy.class);
economy = economyProvider != null ? economyProvider.getProvider() : null;
if (economy == null) {
RegisteredServiceProvider<Permission> permissionProvider = Bukkit.getServer().getServicesManager()
.getRegistration(Permission.class);
permissions = permissionProvider != null ? permissionProvider.getProvider() : null;
enabled = load();
if(!enabled) {
MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not load Vault");
return;
}
MMOItems.plugin.getCrafting().registerCondition("money", config -> new MoneyCondition(economy, config),
MMOItems.plugin.getLogger().log(Level.INFO, "Hooked onto Vault");
MMOItems.plugin.getCrafting().registerCondition("money", config -> new MoneyCondition(config),
new ConditionalDisplay("&a" + AltChar.check + " Requires $#money#",
"&c" + AltChar.cross + " Requires $#money#"));
}
private boolean load() {
if (economy == null)
return false;
if (permissions == null)
return false;
return true;
}
public Permission getPermissions() {
return permissions;
}
public Economy getEconomy() {
return economy;
}
public boolean enabled() {
return enabled;
}
}

View File

@ -0,0 +1,100 @@
package net.Indyuce.mmoitems.stat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.edition.StatEdition;
import net.Indyuce.mmoitems.api.item.build.ItemStackBuilder;
import net.Indyuce.mmoitems.api.item.mmoitem.ReadMMOItem;
import net.Indyuce.mmoitems.gui.edition.EditionInventory;
import net.Indyuce.mmoitems.stat.data.StringListData;
import net.Indyuce.mmoitems.stat.data.random.RandomStatData;
import net.Indyuce.mmoitems.stat.data.type.StatData;
import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.item.ItemTag;
import net.mmogroup.mmolib.api.util.AltChar;
public class GrantedPermissions extends ItemStat implements GemStoneStat {
public GrantedPermissions() {
super("GRANTED_PERMISSIONS", new ItemStack(Material.NAME_TAG), "Granted Permissions", new String[] { "A list of permissions that will,", "be granted by the item." }, new String[] { "all" });
}
@Override
@SuppressWarnings("unchecked")
public StringListData whenInitialized(Object object) {
Validate.isTrue(object instanceof List<?>, "Must specify a string list");
return new StringListData((List<String>) object);
}
@Override
public void whenClicked(EditionInventory inv, InventoryClickEvent event) {
if (event.getAction() == InventoryAction.PICKUP_ALL)
new StatEdition(inv, ItemStat.GRANTED_PERMISSIONS).enable("Write in the chat the permission you want to add.");
if (event.getAction() == InventoryAction.PICKUP_HALF && inv.getEditedSection().contains(getPath())) {
List<String> permissions = inv.getEditedSection().getStringList(getPath());
if (permissions.isEmpty())
return;
String last = permissions.get(permissions.size() - 1);
permissions.remove(last);
inv.getEditedSection().set(getPath(), permissions.isEmpty() ? null : permissions);
inv.registerTemplateEdition();
inv.getPlayer()
.sendMessage(MMOItems.plugin.getPrefix() + "Successfully removed '" + MMOLib.plugin.parseColors(last) + ChatColor.GRAY + "'.");
}
}
@Override
public void whenInput(EditionInventory inv, String message, Object... info) {
List<String> permissions = inv.getEditedSection().contains(getPath()) ? inv.getEditedSection().getStringList(getPath()) : new ArrayList<>();
permissions.add(message);
inv.getEditedSection().set(getPath(), permissions);
inv.registerTemplateEdition();
inv.getPlayer().sendMessage(MMOItems.plugin.getPrefix() + "Permission successfully added.");
}
@Override
public void whenDisplayed(List<String> lore, Optional<RandomStatData> optional) {
if (optional.isPresent()) {
lore.add(ChatColor.GRAY + "Current Value:");
StringListData data = (StringListData) optional.get();
data.getList().forEach(element -> lore.add(ChatColor.GRAY + MMOLib.plugin.parseColors(element)));
} else
lore.add(ChatColor.GRAY + "Current Value: " + ChatColor.RED + "None");
lore.add("");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Click to add a permission.");
lore.add(ChatColor.YELLOW + AltChar.listDash + " Right click to remove the last permission.");
}
@Override
public void whenApplied(ItemStackBuilder item, StatData data) {
JsonArray array = new JsonArray();
((StringListData) data).getList().forEach(line -> array.add(line));
item.addItemTag(new ItemTag(getPath(), array.toString()));
}
@Override
public void whenLoaded(ReadMMOItem mmoitem) {
if (mmoitem.getNBT().hasTag(getNBTPath()))
mmoitem.setData(ItemStat.GRANTED_PERMISSIONS, new StringListData(
new JsonParser().parse(mmoitem.getNBT().getString(getNBTPath())).getAsJsonArray()));
}
}

View File

@ -33,6 +33,7 @@ import net.Indyuce.mmoitems.stat.Elements;
import net.Indyuce.mmoitems.stat.Enchants;
import net.Indyuce.mmoitems.stat.GemColor;
import net.Indyuce.mmoitems.stat.GemSockets;
import net.Indyuce.mmoitems.stat.GrantedPermissions;
import net.Indyuce.mmoitems.stat.HideDye;
import net.Indyuce.mmoitems.stat.HideEnchants;
import net.Indyuce.mmoitems.stat.HidePotionEffects;
@ -215,7 +216,7 @@ public abstract class ItemStat {
public static final ItemStat CAN_DECONSTRUCT = new BooleanStat("CAN_DECONSTRUCT", new ItemStack(Material.PAPER), "Can Deconstruct?",
new String[] { "Players can deconstruct their item", "using this consumable, creating", "another random item." },
new String[] { "consumable" });
public static final ItemStat EFFECTS = new Effects(), PERM_EFFECTS = new PermanentEffects();
public static final ItemStat EFFECTS = new Effects(), PERM_EFFECTS = new PermanentEffects(), GRANTED_PERMISSIONS = new GrantedPermissions();
public static final ItemStat SOULBINDING_CHANCE = new DoubleStat("SOULBINDING_CHANCE", VersionMaterial.ENDER_EYE.toItem(), "Soulbinding Chance",
new String[] { "Defines the chance your item has to", "link another item to your soul,", "preventing other players from using it." },
new String[] { "consumable" });