Implemented SavageFactions Warp and Perms system as requested. - Credits SavageLif3 & ProSavage
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
import com.github.stefvanschie.inventoryframework.Gui;
|
||||
import com.github.stefvanschie.inventoryframework.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.pane.PaginatedPane;
|
||||
import com.massivecraft.factions.Conf;
|
||||
import com.massivecraft.factions.P;
|
||||
import com.massivecraft.factions.integration.Econ;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.Bukkit;
|
||||
import com.massivecraft.factions.zcore.util.TL;
|
||||
import java.util.Map;
|
||||
import com.massivecraft.factions.FPlayer;
|
||||
|
||||
import com.massivecraft.factions.Faction;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public class FactionWarpsFrame {
|
||||
|
||||
private Gui gui;
|
||||
private ConfigurationSection section;
|
||||
|
||||
public FactionWarpsFrame(final Faction f) {
|
||||
this.section = P.p.getConfig().getConfigurationSection("fwarp-gui");
|
||||
this.gui = new Gui(P.p, section.getInt("rows", 3), P.p.color(this.section.getString("name").replace("{faction}",f.getTag())));
|
||||
}
|
||||
|
||||
public void buildGUI(final FPlayer fplayer) {
|
||||
final PaginatedPane pane = new PaginatedPane(0, 0, 9, this.gui.getRows());
|
||||
final List<GuiItem> GUIItems = new ArrayList<>();
|
||||
final List<Integer> slots = section.getIntegerList("warp-slots");
|
||||
int count = 0;
|
||||
for (int x = 0; x <= gui.getRows() * 9 - 1; ++x) GUIItems.add(new GuiItem(buildDummyItem(), e -> e.setCancelled(true)));
|
||||
slots.forEach(slot -> GUIItems.set(slot, new GuiItem(XMaterial.AIR.parseItem())));
|
||||
for (final Map.Entry<String, LazyLocation> warp : fplayer.getFaction().getWarps().entrySet()) {
|
||||
if (count > slots.size()) continue;
|
||||
GUIItems.set(slots.get(count), new GuiItem(buildWarpAsset(warp, fplayer.getFaction()), e -> {
|
||||
e.setCancelled(true);
|
||||
fplayer.getPlayer().closeInventory();
|
||||
|
||||
if (!fplayer.getFaction().hasWarpPassword(warp.getKey())) {
|
||||
if (transact(fplayer)) {
|
||||
doWarmup(warp.getKey(), fplayer);
|
||||
}
|
||||
} else {
|
||||
fplayer.setEnteringPassword(true, warp.getKey());
|
||||
fplayer.msg(TL.COMMAND_FWARP_PASSWORD_REQUIRED);
|
||||
Bukkit.getScheduler().runTaskLater(P.p, () -> {
|
||||
if (fplayer.isEnteringPassword()) {
|
||||
fplayer.msg(TL.COMMAND_FWARP_PASSWORD_TIMEOUT);
|
||||
fplayer.setEnteringPassword(false, "");
|
||||
}
|
||||
}, P.p.getConfig().getInt("fwarp-gui.password-timeout", 5) * 20);
|
||||
}
|
||||
}));
|
||||
++count;
|
||||
}
|
||||
pane.populateWithGuiItems(GUIItems);
|
||||
gui.addPane(pane);
|
||||
gui.update();
|
||||
gui.show(fplayer.getPlayer());
|
||||
}
|
||||
|
||||
private ItemStack buildWarpAsset(final Map.Entry<String, LazyLocation> warp, final Faction faction) {
|
||||
final ConfigurationSection config = this.section.getConfigurationSection("warp-item");
|
||||
final ItemStack item = XMaterial.matchXMaterial(config.getString("Type")).parseItem();
|
||||
final ItemMeta meta = item.getItemMeta();
|
||||
meta.setLore(P.p.colorList(P.p.replacePlaceholders(config.getStringList("Lore"), new Placeholder("{warp-protected}", faction.hasWarpPassword(warp.getKey()) ? "Enabled" : "Disabled"), new Placeholder("{warp-cost}", P.p.getConfig().getBoolean("warp-cost.enabled", false) ? Integer.toString(P.p.getConfig().getInt("warp-cost.warp", 5)) : "Disabled"))));
|
||||
meta.setDisplayName(P.p.color(config.getString("Name").replace("{warp}", warp.getKey())));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private ItemStack buildDummyItem() {
|
||||
final ConfigurationSection config = this.section.getConfigurationSection("dummy-item");
|
||||
final ItemStack item = XMaterial.matchXMaterial(config.getString("Type")).parseItem();
|
||||
final ItemMeta meta = item.getItemMeta();
|
||||
meta.setLore(P.p.colorList(config.getStringList("Lore")));
|
||||
meta.setDisplayName(P.p.color(config.getString("Name")));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
|
||||
private void doWarmup(final String warp, FPlayer fme) {
|
||||
WarmUpUtil.process(fme, WarmUpUtil.Warmup.WARP, TL.WARMUPS_NOTIFY_TELEPORT, warp, () -> {
|
||||
Player player = Bukkit.getPlayer(fme.getPlayer().getUniqueId());
|
||||
if (player != null) {
|
||||
player.teleport(fme.getFaction().getWarp(warp).getLocation());
|
||||
fme.msg(TL.COMMAND_FWARP_WARPED, warp);
|
||||
}
|
||||
}, P.p.getConfig().getLong("warmups.f-warp", 0));
|
||||
}
|
||||
|
||||
private boolean transact(FPlayer player) {
|
||||
if (!P.p.getConfig().getBoolean("warp-cost.enabled", false) || player.isAdminBypassing()) return true;
|
||||
double cost = P.p.getConfig().getDouble("warp-cost.warp", 5);
|
||||
if (!Econ.shouldBeUsed() || cost == 0.0 || player.isAdminBypassing()) return true;
|
||||
|
||||
if (Conf.bankEnabled && Conf.bankFactionPaysCosts && player.hasFaction()) {
|
||||
return Econ.modifyMoney(player.getFaction(), -cost, TL.COMMAND_FWARP_TOWARP.toString(), TL.COMMAND_FWARP_FORWARPING.toString());
|
||||
} else {
|
||||
return Econ.modifyMoney(player, -cost, TL.COMMAND_FWARP_TOWARP.toString(), TL.COMMAND_FWARP_FORWARPING.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.massivecraft.factions.util.Particles;
|
||||
|
||||
import com.massivecraft.factions.P;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
|
||||
public enum Particles {
|
||||
|
||||
|
||||
// Gotta use Strings or runtime errors on 1.8, the Particle class does not exist
|
||||
CLOUD(ParticleEffect.CLOUD, "CLOUD"),
|
||||
REDSTONE(ParticleEffect.REDSTONE, "REDSTONE"),
|
||||
NOTE(ParticleEffect.NOTE, "NOTE");
|
||||
|
||||
|
||||
private ParticleEffect sub18;
|
||||
private String over19;
|
||||
|
||||
Particles(ParticleEffect sub18, String over19) {
|
||||
this.sub18 = sub18;
|
||||
this.over19 = over19;
|
||||
}
|
||||
|
||||
|
||||
public void displayAtLocation(Location location, int amt) {
|
||||
if (P.p.useNonPacketParticles) {
|
||||
// 1.9+ based servers will use the built in particleAPI instead of packet based.
|
||||
// any particle amount higher than 0 made them go everywhere, and the offset at 0 was not working.
|
||||
// So setting the amount to 0 spawns 1 in the precise location
|
||||
location.getWorld().spawnParticle(Particle.valueOf(over19), location, 0);
|
||||
} else {
|
||||
sub18.display((float) 0, (float) 0, (float) 0, (float) 0, amt, location, 16);
|
||||
}
|
||||
}
|
||||
|
||||
public void displayAtLocation(Location location, int amt, ParticleEffect.OrdinaryColor color) {
|
||||
if (P.p.useNonPacketParticles) {
|
||||
// 1.9-1.11 & 1.13+ based servers will use the built in particleAPI instead of packet based.
|
||||
// any particle amount higher than 0 made them go everywhere, and the offset at 0 was not working.
|
||||
// So setting the amount to 0 spawns 1 in the precise location
|
||||
|
||||
|
||||
// Gotta do this so colorable ones have their data :P
|
||||
if (this == Particles.REDSTONE || this == Particles.CLOUD || this == Particles.NOTE) {
|
||||
if (P.p.mc112) {
|
||||
location.getWorld().spawnParticle(Particle.valueOf(over19), location, 0);
|
||||
} else {
|
||||
location.getWorld().spawnParticle(Particle.valueOf(over19), location, 0, new Particle.DustOptions(Color.fromRGB(color.getRed(), color.getGreen(), color.getBlue()), 1));
|
||||
}
|
||||
} else {
|
||||
location.getWorld().spawnParticle(Particle.valueOf(over19), location, 0);
|
||||
}
|
||||
} else {
|
||||
sub18.display(color, location, 16);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
public class Placeholder {
|
||||
|
||||
private String tag;
|
||||
private String replace;
|
||||
|
||||
public Placeholder(String tag, String replace) {
|
||||
this.tag = tag;
|
||||
this.replace = replace;
|
||||
}
|
||||
|
||||
public String getReplace() {
|
||||
return replace;
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,259 +0,0 @@
|
||||
package com.massivecraft.factions.util;
|
||||
|
||||
import com.massivecraft.factions.Conf;
|
||||
import com.massivecraft.factions.FPlayer;
|
||||
import com.massivecraft.factions.Faction;
|
||||
import com.massivecraft.factions.P;
|
||||
import com.massivecraft.factions.integration.Econ;
|
||||
import com.massivecraft.factions.zcore.util.TL;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class WarpGUI implements InventoryHolder, FactionGUI {
|
||||
|
||||
private final ConfigurationSection section;
|
||||
int guiSize;
|
||||
private Inventory warpGUI;
|
||||
private FPlayer fme;
|
||||
private HashMap<Integer, String> warpSlots = new HashMap<>();
|
||||
private int maxWarps;
|
||||
private List<Integer> dummySlots = new ArrayList<>();
|
||||
|
||||
public WarpGUI(FPlayer fme) {
|
||||
this.fme = fme;
|
||||
this.section = P.p.getConfig().getConfigurationSection("fwarp-gui");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void build() {
|
||||
if (section == null) {
|
||||
P.p.log(Level.WARNING, "Attempted to build f warp GUI but config section not present.");
|
||||
P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Build basic Inventory info
|
||||
guiSize = section.getInt("rows", 3);
|
||||
if (guiSize > 6) {
|
||||
guiSize = 6;
|
||||
P.p.log(Level.INFO, "Warp GUI size out of bounds, defaulting to 6");
|
||||
}
|
||||
|
||||
guiSize *= 9;
|
||||
String guiName = ChatColor.translateAlternateColorCodes('&', section.getString("name", "FactionPermissions"));
|
||||
warpGUI = Bukkit.createInventory(this, guiSize, guiName);
|
||||
|
||||
maxWarps = P.p.getConfig().getInt("max-warps", 5);
|
||||
|
||||
Set<String> factionWarps = fme.getFaction().getWarps().keySet();
|
||||
List<Integer> warpOpenSlots = section.getIntegerList("warp-slots");
|
||||
|
||||
buildDummyItems();
|
||||
|
||||
if (maxWarps != warpOpenSlots.size()) {
|
||||
P.p.log(Level.SEVERE, "Invalid warp slots for GUI, Please use same value as max warps");
|
||||
return;
|
||||
}
|
||||
|
||||
int warpSlotIndex = 0;
|
||||
for (String warp : factionWarps) {
|
||||
warpSlots.put(warpOpenSlots.get(warpSlotIndex), warp);
|
||||
warpSlotIndex++;
|
||||
}
|
||||
|
||||
buildItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return warpGUI;
|
||||
}
|
||||
|
||||
private void buildItems() {
|
||||
for (Map.Entry<Integer, String> entry : warpSlots.entrySet()) {
|
||||
warpGUI.setItem(entry.getKey(), buildItem(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(int slot, ClickType action) {
|
||||
if (warpSlots.containsKey(slot)) {
|
||||
fme.getPlayer().closeInventory();
|
||||
|
||||
// All clear lets TP them or ask for password
|
||||
String warp = warpSlots.get(slot);
|
||||
if (!fme.getFaction().hasWarpPassword(warp)) {
|
||||
if (transact(fme)) {
|
||||
doWarmup(warp);
|
||||
}
|
||||
} else {
|
||||
fme.setEnteringPassword(true, warp);
|
||||
fme.msg(TL.COMMAND_FWARP_PASSWORD_REQUIRED);
|
||||
Bukkit.getScheduler().runTaskLater(P.p, () -> {
|
||||
if (fme.isEnteringPassword()) {
|
||||
fme.msg(TL.COMMAND_FWARP_PASSWORD_TIMEOUT);
|
||||
fme.setEnteringPassword(false, "");
|
||||
}
|
||||
}, P.p.getConfig().getInt("fwarp-gui.password-timeout", 5) * 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void doWarmup(final String warp) {
|
||||
WarmUpUtil.process(fme, WarmUpUtil.Warmup.WARP, TL.WARMUPS_NOTIFY_TELEPORT, warp, () -> {
|
||||
Player player = Bukkit.getPlayer(fme.getPlayer().getUniqueId());
|
||||
if (player != null) {
|
||||
player.teleport(fme.getFaction().getWarp(warp).getLocation());
|
||||
fme.msg(TL.COMMAND_FWARP_WARPED, warp);
|
||||
}
|
||||
}, P.p.getConfig().getLong("warmups.f-warp", 0));
|
||||
}
|
||||
|
||||
private boolean transact(FPlayer player) {
|
||||
if (!P.p.getConfig().getBoolean("warp-cost.enabled", false) || player.isAdminBypassing()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
double cost = P.p.getConfig().getDouble("warp-cost.warp", 5);
|
||||
|
||||
if (!Econ.shouldBeUsed() || this.fme == null || cost == 0.0 || fme.isAdminBypassing()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Conf.bankEnabled && Conf.bankFactionPaysCosts && fme.hasFaction()) {
|
||||
return Econ.modifyMoney(fme.getFaction(), -cost, TL.COMMAND_FWARP_TOWARP.toString(), TL.COMMAND_FWARP_FORWARPING.toString());
|
||||
} else {
|
||||
return Econ.modifyMoney(fme, -cost, TL.COMMAND_FWARP_TOWARP.toString(), TL.COMMAND_FWARP_FORWARPING.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack buildItem(String warp) {
|
||||
ConfigurationSection warpItemSection = section.getConfigurationSection("warp-item");
|
||||
if (warpItemSection == null) {
|
||||
P.p.log(Level.WARNING, "Attempted to build f warp GUI but config section not present.");
|
||||
P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config.");
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
|
||||
|
||||
String displayName = replacePlaceholers(warpItemSection.getString("name"), warp, fme.getFaction());
|
||||
List<String> lore = new ArrayList<>();
|
||||
|
||||
if (warpItemSection.getString("material") == null) {
|
||||
return null;
|
||||
}
|
||||
Material material = XMaterial.matchXMaterial("material").parseMaterial();
|
||||
if (material == null) {
|
||||
material = Material.STONE;
|
||||
}
|
||||
|
||||
ItemStack item = new ItemStack(material);
|
||||
ItemMeta itemMeta = item.getItemMeta();
|
||||
|
||||
for (String loreLine : warpItemSection.getStringList("lore")) {
|
||||
lore.add(replacePlaceholers(loreLine, warp, fme.getFaction()));
|
||||
}
|
||||
|
||||
itemMeta.setDisplayName(displayName);
|
||||
itemMeta.setLore(lore);
|
||||
item.setItemMeta(itemMeta);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private String replacePlaceholers(String string, String warp, Faction faction) {
|
||||
string = ChatColor.translateAlternateColorCodes('&', string);
|
||||
string = string.replace("{warp}", warp);
|
||||
string = string.replace("{warp-protected}", faction.hasWarpPassword(warp) ? "Enabled" : "Disabled");
|
||||
string = string.replace("{warp-cost}", !P.p.getConfig().getBoolean("warp-cost.enabled", false) ? "Disabled" : Integer.toString(P.p.getConfig().getInt("warp-cost.warp", 5)));
|
||||
return string;
|
||||
}
|
||||
|
||||
private void buildDummyItems() {
|
||||
for (String key : section.getConfigurationSection("dummy-slots").getKeys(false)) {
|
||||
int dummyId;
|
||||
try {
|
||||
dummyId = Integer.parseInt(key);
|
||||
} catch (NumberFormatException exception) {
|
||||
P.p.log(Level.WARNING, "Invalid dummy item id: " + key.toUpperCase());
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack dummyItem = buildDummyItem(dummyId);
|
||||
if (dummyItem == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Integer> dummyIdSlots = section.getIntegerList("dummy-slots." + key);
|
||||
for (Integer slot : dummyIdSlots) {
|
||||
if (slot + 1 > guiSize || slot < 0) {
|
||||
P.p.log(Level.WARNING, "Invalid slot: " + slot + " for dummy item: " + key);
|
||||
continue;
|
||||
}
|
||||
dummySlots.add(slot);
|
||||
warpGUI.setItem(slot, dummyItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ItemStack buildDummyItem(int id) {
|
||||
final ConfigurationSection dummySection = section.getConfigurationSection("dummy-items." + id);
|
||||
|
||||
if (dummySection == null) {
|
||||
P.p.log(Level.WARNING, "Attempted to build f warp GUI but config section not present.");
|
||||
P.p.log(Level.WARNING, "Copy your config, allow the section to generate, then copy it back to your old config.");
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
|
||||
Material material = Material.matchMaterial(dummySection.getString("material", ""));
|
||||
if (material == null) {
|
||||
P.p.log(Level.WARNING, "Invalid material for dummy item: " + id);
|
||||
return null;
|
||||
}
|
||||
|
||||
ItemStack itemStack = new ItemStack(material);
|
||||
|
||||
DyeColor color;
|
||||
try {
|
||||
color = DyeColor.valueOf(dummySection.getString("color", ""));
|
||||
} catch (Exception exception) {
|
||||
color = null;
|
||||
}
|
||||
if (color != null) {
|
||||
itemStack.setDurability(color.getWoolData());
|
||||
}
|
||||
|
||||
ItemMeta itemMeta = itemStack.getItemMeta();
|
||||
|
||||
if (!P.p.mc17) {
|
||||
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_ATTRIBUTES);
|
||||
}
|
||||
|
||||
|
||||
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', dummySection.getString("name", " ")));
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
for (String loreLine : dummySection.getStringList("lore")) {
|
||||
lore.add(ChatColor.translateAlternateColorCodes('&', loreLine));
|
||||
}
|
||||
itemMeta.setLore(lore);
|
||||
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user