Implemented basic Flags API

Removed Island's SettingsFlags
Fixed Island#toggleFlag()
This commit is contained in:
Florian CUNY 2018-01-01 01:41:19 +01:00
parent 1d3c82d1c7
commit ae0dc7c538
5 changed files with 137 additions and 232 deletions

View File

@ -20,6 +20,7 @@ import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.PanelListenerManager; import us.tastybento.bskyblock.listeners.PanelListenerManager;
import us.tastybento.bskyblock.managers.AddonsManager; import us.tastybento.bskyblock.managers.AddonsManager;
import us.tastybento.bskyblock.managers.CommandsManager; import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.FlagsManager;
import us.tastybento.bskyblock.managers.LocalesManager; import us.tastybento.bskyblock.managers.LocalesManager;
/** /**
@ -42,6 +43,7 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
private CommandsManager commandsManager; private CommandsManager commandsManager;
private LocalesManager localesManager; private LocalesManager localesManager;
private AddonsManager addonsManager; private AddonsManager addonsManager;
private FlagsManager flagsManager;
@Override @Override
public void onEnable(){ public void onEnable(){
@ -101,6 +103,9 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
// Register Listeners // Register Listeners
registerListeners(); registerListeners();
// Load Flags
flagsManager = new FlagsManager();
// Load addons // Load addons
addonsManager = new AddonsManager(); addonsManager = new AddonsManager();
addonsManager.enableAddons(); addonsManager.enableAddons();
@ -204,14 +209,34 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
return plugin; return plugin;
} }
/**
* @return the Commands manager
*/
public CommandsManager getCommandsManager() { public CommandsManager getCommandsManager() {
return commandsManager; return commandsManager;
} }
/**
* @return the Locales manager
*/
public LocalesManager getLocalesManager() { public LocalesManager getLocalesManager() {
return localesManager; return localesManager;
} }
/**
* @return the Addons manager
*/
public AddonsManager getAddonsManager() {
return addonsManager;
}
/**
* @return the Flags manager
*/
public FlagsManager getFlagsManager() {
return flagsManager;
}
@Override @Override
public final String getIdentifier() { public final String getIdentifier() {
return getDescription().getName(); return getDescription().getName();
@ -226,11 +251,4 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
public final File getFolder() { public final File getFolder() {
return getDataFolder(); return getDataFolder();
} }
/**
* @return the addonsManager
*/
public AddonsManager getAddonsManager() {
return addonsManager;
}
} }

View File

@ -0,0 +1,31 @@
package us.tastybento.bskyblock.api.flags;
import org.bukkit.event.Listener;
import us.tastybento.bskyblock.api.panels.PanelItem;
import java.util.Optional;
public class Flag {
private String id;
private PanelItem icon;
private Optional<Listener> listener;
public Flag(String id, PanelItem icon, Optional<Listener> listener) {
this.id = id;
this.icon = icon;
this.listener = listener;
}
public String getID() {
return id;
}
public PanelItem getIcon() {
return icon;
}
public Optional<Listener> getListener() {
return listener;
}
}

View File

@ -0,0 +1,32 @@
package us.tastybento.bskyblock.api.flags;
import org.bukkit.event.Listener;
import us.tastybento.bskyblock.api.panels.PanelItem;
import java.util.Optional;
public class FlagBuilder {
private String id;
private PanelItem icon;
private Optional<Listener> listener = Optional.empty();
public FlagBuilder id(String id) {
this.id = id;
return this;
}
public FlagBuilder icon(PanelItem icon) {
this.icon = icon;
return this;
}
public FlagBuilder listener(Listener listener) {
this.listener = Optional.of(listener);
return this;
}
public void build() {
new Flag(id, icon, listener);
}
}

View File

@ -18,6 +18,7 @@ import us.tastybento.bskyblock.api.events.island.IslandEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason; import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.config.Settings; import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
@ -47,218 +48,6 @@ public class Island extends DataObject {
this.uniqueId = uniqueId; this.uniqueId = uniqueId;
} }
/**
* Island Guard Settings flags
* Covers island, spawn and system settings
*
* @author Tastybento
*/
public enum SettingsFlag{
ACID_DAMAGE,
// Can use Anvil
ANVIL,
// Can interact with Armor Stand
ARMOR_STAND,
// Can interact with Beacon
BEACON,
// Can use bed
BED,
// Can break blocks
BREAK_BLOCKS,
// Can breed animals
BREEDING,
// Can use brewing stand
BREWING,
// Can use buttons
BUTTON,
// Can empty or fill buckets
BUCKET,
// Can collect lava (override BUCKET)
COLLECT_LAVA,
// Can collect water (override BUCKET)
COLLECT_WATER,
// Can eat and teleport with Chorus Fruit
CHORUS_FRUIT,
// Can use the workbench
CRAFTING,
// Allow creepers to hurt entities (but not to destroy blocks)
CREEPER_HURT,
// Allow monsters, e.g. creepers, ghasts, withers to destroy blocks, including item frames
MONSTER_GRIEFING,
// Allow monsters to blow up any inventory block, e.g. chests, dispenser, shulker box
MONSTER_BLOW_UP_CHEST,
// Can trample crops
CROP_TRAMPLE,
// Can open doors
DOOR,
// Can open trapdoors, iron or wood
TRAPDOOR,
// Can dye sheep
DYEING,
// Can use Elytra
ELYTRA,
// Can use the enchanting table
ENCHANTING,
// Allow Enderman griefing
ENDERMAN_GRIEFING,
// Display enter/exit island messages
ENTER_EXIT_MESSAGES,
// Fire use/placement in general
FIRE,
// Can extinguish fires by punching them
FIRE_EXTINGUISH,
// Allow fire spread
FIRE_SPREAD,
// Can use fishing rod
FISHING_ROD,
// Can use furnaces
FURNACE,
// Can open gates
GATE,
// Can hurt animals (e.g. cows) - Villagers excluded
HURT_ANIMALS,
// Can hurt monsters
HURT_MONSTERS,
// Can hurt villagers
HURT_VILLAGERS,
// Can ignite creepers using flint and steel
IGNITE_CREEPER,
// Can ignite TNTs using flint and steel
IGNITE_TNT,
// Can interact with tamed animals
INTERACT_TAMED,
// Can drop items
ITEM_DROP,
// Can pickup items
ITEM_PICKUP,
// Keep inventory on death
KEEP_INVENTORY,
// Can leash or unleash animals
LEASH,
// Can use levers
LEVER,
// Can milk cows
MILKING,
// Animals can spawn
ANIMAL_SPAWN,
// Monsters can spawn
MONSTER_SPAWN,
// Can open horse or other animal inventories (llama)
MOUNT_INVENTORY,
// Can ride an animal
MOUNT_RIDING,
// Can operate jukeboxes, noteblocks
MUSIC,
// Can open chests or other inventory blocks, e.g., dispensers, droppers, hoppers, etc.
OPEN_CHESTS,
// Can place blocks
PLACE_BLOCKS,
// Can go through portals
PORTAL,
// Can activate pressure plates
PRESSURE_PLATE,
// Can do PvP in the overworld
PVP_OVERWORLD,
// Can do PvP in the nether
PVP_NETHER,
// Can do PvP in the end
PVP_END,
// Can interact with redstone items (repeaters, comparators)
REDSTONE,
// Can use spawn eggs
SPAWN_EGGS,
// Can shear sheep
SHEARING,
// Can throw chicken eggs
THROW_EGGS,
// Can throw fireworks
THROW_FIREWORKS,
// Can throw enderpearls
THROW_ENDERPEARLS,
// Can throw snowballs
THROW_SNOWBALLS,
// Can throw splash potions
THROW_SPLASH_POTIONS,
// Can throw lingering potions
THROW_LINGERING_POTIONS,
// Allow TNT to hurt entities (but not to destroy blocks)
TNT_HURT,
// Allow TNT to destroy blocks
TNT_GRIEFING,
// Allow TNTs to blow up any chest or inventory block (only if TNT_griefing is enabled)
TNT_BLOW_UP_CHEST,
// Can trade with villagers
VILLAGER_TRADING
}
//// Island //// //// Island ////
// The center of the island itself // The center of the island itself
private Location center; private Location center;
@ -308,7 +97,7 @@ public class Island extends DataObject {
private boolean spawn = false; private boolean spawn = false;
private boolean purgeProtected = false; private boolean purgeProtected = false;
//// Protection //// //// Protection ////
private HashMap<SettingsFlag, Boolean> flags = new HashMap<>(); private HashMap<Flag, Boolean> flags = new HashMap<>();
private int levelHandicap; private int levelHandicap;
@ -394,14 +183,10 @@ public class Island extends DataObject {
* @param flag * @param flag
* @return true or false, or false if flag is not in the list * @return true or false, or false if flag is not in the list
*/ */
public boolean getFlag(SettingsFlag flag){ public boolean getFlag(Flag flag){
if(flags.containsKey(flag)) { if(flags.containsKey(flag)) {
return flags.get(flag); return flags.get(flag);
} else { } else {
if (flag.equals(SettingsFlag.ANIMAL_SPAWN) || flag.equals(SettingsFlag.MONSTER_SPAWN)) {
flags.put(flag, true);
return true;
}
flags.put(flag, false); flags.put(flag, false);
return false; return false;
} }
@ -410,7 +195,7 @@ public class Island extends DataObject {
/** /**
* @return the flags * @return the flags
*/ */
public HashMap<SettingsFlag, Boolean> getFlags() { public HashMap<Flag, Boolean> getFlags() {
return flags; return flags;
} }
@ -632,14 +417,14 @@ public class Island extends DataObject {
* @param flag * @param flag
* @param value * @param value
*/ */
public void setFlag(SettingsFlag flag, boolean value){ public void setFlag(Flag flag, boolean value){
flags.put(flag, value); flags.put(flag, value);
} }
/** /**
* @param flags the flags to set * @param flags the flags to set
*/ */
public void setFlags(HashMap<SettingsFlag, Boolean> flags) { public void setFlags(HashMap<Flag, Boolean> flags) {
this.flags = flags; this.flags = flags;
} }
@ -792,9 +577,9 @@ public class Island extends DataObject {
* Toggles the Island Guard flag status if it is in the list * Toggles the Island Guard flag status if it is in the list
* @param flag * @param flag
*/ */
public void toggleFlag(SettingsFlag flag){ public void toggleFlag(Flag flag){
if(flags.containsKey(flag)) { if(flags.containsKey(flag)) {
flags.put(flag, (flags.get(flag))); flags.put(flag, !flags.get(flag));
} }
} }

View File

@ -0,0 +1,39 @@
package us.tastybento.bskyblock.managers;
import org.bukkit.Bukkit;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.panels.PanelItem;
import java.util.ArrayList;
import java.util.List;
public final class FlagsManager {
private List<Flag> flags = new ArrayList<>();
public void registerFlag(Flag flag) {
//TODO all the security checks
flags.add(flag);
// If there is a listener, register it into Bukkit.
flag.getListener().ifPresent(l -> Bukkit.getPluginManager().registerEvents(l, BSkyBlock.getInstance()));
}
public List<Flag> getFlags() {
return flags;
}
public Flag getFlagByID(String id) {
for (Flag flag : flags) {
if (flag.getID().equals(id.toUpperCase())) return flag;
}
return null;
}
public Flag getFlagByIcon(PanelItem item) {
for (Flag flag : flags) {
if (flag.getIcon().equals(item)) return flag;
}
return null;
}
}