Added Flag.Builder

This commit is contained in:
Florian CUNY 2018-12-23 16:33:10 +01:00
parent 0101ed86d3
commit 168f9ae8f1

View File

@ -9,6 +9,9 @@ import org.bukkit.inventory.ItemStack;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick;
import world.bentobox.bentobox.api.flags.clicklisteners.IslandToggleClick;
import world.bentobox.bentobox.api.flags.clicklisteners.WorldToggleClick;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
@ -45,6 +48,10 @@ public class Flag implements Comparable<Flag> {
private final PanelItem.ClickHandler clickHandler;
private final boolean subPanel;
/**
* @deprecated {@link Flag.Builder} should be used instead.
*/
@Deprecated
Flag(String id, Material icon, Listener listener, Type type, int defaultRank, PanelItem.ClickHandler clickListener, boolean subPanel) {
this.id = id;
this.icon = icon;
@ -55,6 +62,17 @@ public class Flag implements Comparable<Flag> {
this.subPanel = subPanel;
}
private Flag(Builder builder) {
this.id = builder.id;
this.icon = builder.icon;
this.listener = builder.listener;
this.type = builder.type;
this.setting = builder.defaultSetting;
this.defaultRank = builder.defaultRank;
this.clickHandler = builder.clickHandler;
this.subPanel = builder.usePanel;
}
public String getID() {
return id;
}
@ -247,4 +265,86 @@ public class Flag implements Comparable<Flag> {
public int compareTo(Flag o) {
return getID().compareTo(o.getID());
}
/**
* @author tastybento, Poslovitch
*/
public static class Builder {
// Mandatory fields
private String id;
private Material icon;
// Listener
private Listener listener;
// Type - is defaulted to PROTECTION
private Type type = Type.PROTECTION;
// Default settings
private boolean defaultSetting;
private int defaultRank;
// ClickHandler - default depends on the type
private PanelItem.ClickHandler clickHandler;
// Whether there is a sub-panel or not
private boolean usePanel = false;
public Builder(String id, Material icon) {
this.id = id;
this.icon = icon;
}
public Builder listener(Listener listener) {
this.listener = listener;
return this;
}
public Builder type(Type type) {
this.type = type;
return this;
}
public Builder clickHandler(PanelItem.ClickHandler clickHandler) {
this.clickHandler = clickHandler;
return this;
}
public Builder defaultSetting(boolean defaultSetting) {
this.defaultSetting = defaultSetting;
return this;
}
public Builder defaultRank(int defaultRank) {
this.defaultRank = defaultRank;
return this;
}
private Builder usePanel(boolean usePanel) {
this.usePanel = usePanel;
return this;
}
public Flag build() {
// If no clickHandler has been set, then apply default ones
if (clickHandler == null) {
switch (type){
case PROTECTION:
clickHandler = new CycleClick(id);
break;
case SETTING:
clickHandler = new IslandToggleClick(id);
break;
case WORLD_SETTING:
clickHandler = new WorldToggleClick(id);
break;
default:
clickHandler = new CycleClick(id);
break;
}
}
return new Flag(this);
}
}
}