Skip to content

Commit

Permalink
Merge pull request #409 from BentoBoxWorld/flag-builder-v2
Browse files Browse the repository at this point in the history
Nesting Flag builder in Flag
  • Loading branch information
Poslovitch authored Dec 24, 2018
2 parents a321045 + 68d6b00 commit d24d614
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 101 deletions.
99 changes: 99 additions & 0 deletions src/main/java/world/bentobox/bentobox/api/flags/Flag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

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;
Expand Down Expand Up @@ -45,6 +48,9 @@ public Material getIcon() {
private final PanelItem.ClickHandler clickHandler;
private final boolean subPanel;

/**
* {@link Flag.Builder} should be used instead. This is only used for testing.
*/
Flag(String id, Material icon, Listener listener, Type type, int defaultRank, PanelItem.ClickHandler clickListener, boolean subPanel) {
this.id = id;
this.icon = icon;
Expand All @@ -55,6 +61,17 @@ public Material getIcon() {
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;
}
Expand Down Expand Up @@ -247,4 +264,86 @@ public String toString() {
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 = RanksManager.MEMBER_RANK;

// 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;
}

public 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.managers.RanksManager;

/**
* @deprecated Replaced by {@link Flag.Builder}.
*/
@Deprecated
public class FlagBuilder {

private String id;
Expand Down
Loading

0 comments on commit d24d614

Please sign in to comment.