-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds CommandDirector Adds BlobObjectBuilder
- Loading branch information
1 parent
86f80e2
commit 2b698a5
Showing
13 changed files
with
935 additions
and
744 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/us/mytheria/bloblib/entities/BuilderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package us.mytheria.bloblib.entities; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.inventory.InventoryType; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.inventory.Inventory; | ||
import org.jetbrains.annotations.NotNull; | ||
import us.mytheria.bloblib.api.BlobLibInventoryAPI; | ||
import us.mytheria.bloblib.entities.inventory.BlobInventory; | ||
import us.mytheria.bloblib.entities.inventory.BlobObjectBuilder; | ||
import us.mytheria.bloblib.managers.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
public abstract class BuilderManager<T extends BlobObject, B extends BlobObjectBuilder<T>> extends Manager implements Listener { | ||
protected String title; | ||
|
||
protected Map<UUID, B> builders; | ||
protected Map<Inventory, B> inventories; | ||
private final ChatListenerManager chatManager; | ||
private final DropListenerManager dropListenerManager; | ||
private final SelectorListenerManager selectorListenerManager; | ||
private final SelPosListenerManager selPosListenerManager; | ||
private final String fileKey; | ||
|
||
public BuilderManager(ManagerDirector managerDirector, | ||
String fileKey) { | ||
super(managerDirector); | ||
Bukkit.getPluginManager().registerEvents(this, getPlugin()); | ||
selPosListenerManager = getManagerDirector().getPositionListenerManager(); | ||
chatManager = getManagerDirector().getChatListenerManager(); | ||
dropListenerManager = getManagerDirector().getDropListenerManager(); | ||
selectorListenerManager = getManagerDirector().getSelectorManager(); | ||
this.fileKey = Objects.requireNonNull(fileKey, "File key cannot be null."); | ||
update(); | ||
} | ||
|
||
@EventHandler | ||
public void onQuit(PlayerQuitEvent event) { | ||
removeBuilder(event.getPlayer()); | ||
} | ||
|
||
@EventHandler | ||
public void onClick(InventoryClickEvent event) { | ||
Inventory clicked = event.getClickedInventory(); | ||
if (clicked == null) | ||
return; | ||
if (clicked.getType() == InventoryType.PLAYER) | ||
return; | ||
if (!inventories.containsKey(clicked)) | ||
return; | ||
int slot = event.getRawSlot(); | ||
Player player = (Player) event.getWhoClicked(); | ||
BlobObjectBuilder<T> builder = getOrDefault(player.getUniqueId()); | ||
if (slot >= builder.getSize()) { | ||
return; | ||
} | ||
event.setCancelled(true); | ||
builder.handle(slot, player); | ||
} | ||
|
||
@Override | ||
public void reload() { | ||
update(); | ||
} | ||
|
||
public void update() { | ||
this.builders = new HashMap<>(); | ||
this.inventories = new HashMap<>(); | ||
BlobInventory inventory = BlobLibInventoryAPI.getInstance().getBlobInventory(fileKey); | ||
if (inventory == null) | ||
throw new RuntimeException("Inventory file '" + fileKey + "' not found."); | ||
this.title = inventory.getTitle(); | ||
} | ||
|
||
@NotNull | ||
public abstract B getOrDefault(UUID uuid); | ||
|
||
@NotNull | ||
public B getOrDefault(Player player) { | ||
return getOrDefault(player.getUniqueId()); | ||
} | ||
|
||
@NotNull | ||
public BuilderManager<T, B> addBuilder(UUID uuid, B builder) { | ||
builders.put(uuid, builder); | ||
inventories.put(builder.getInventory(), builder); | ||
return this; | ||
} | ||
|
||
@NotNull | ||
public BuilderManager<T, B> addBuilder(Player player, B builder) { | ||
addBuilder(player.getUniqueId(), builder); | ||
return this; | ||
} | ||
|
||
@NotNull | ||
public BuilderManager<T, B> removeBuilder(UUID uuid) { | ||
B builder = builders.get(uuid); | ||
if (builder == null) | ||
return this; | ||
Inventory key = builder.getInventory(); | ||
inventories.remove(key); | ||
builders.remove(uuid); | ||
return this; | ||
} | ||
|
||
@NotNull | ||
public BuilderManager<T, B> removeBuilder(Player player) { | ||
removeBuilder(player.getUniqueId()); | ||
return this; | ||
} | ||
|
||
@NotNull | ||
public String getFileKey() { | ||
return fileKey; | ||
} | ||
|
||
@NotNull | ||
public DropListenerManager getDropListenerManager() { | ||
return dropListenerManager; | ||
} | ||
|
||
@NotNull | ||
public ChatListenerManager getChatManager() { | ||
return chatManager; | ||
} | ||
|
||
@NotNull | ||
public SelectorListenerManager getSelectorListenerManager() { | ||
return selectorListenerManager; | ||
} | ||
|
||
@NotNull | ||
public SelPosListenerManager getSelPosListenerManager() { | ||
return selPosListenerManager; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/us/mytheria/bloblib/entities/CommandDirector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package us.mytheria.bloblib.entities; | ||
|
||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class CommandDirector { | ||
private final BlobExecutor executor; | ||
private final List<Function<ExecutorData, Boolean>> nonAdminChildCommands; | ||
private final List<Function<ExecutorData, Boolean>> adminChildCommands; | ||
private final List<Function<ExecutorData, List<String>>> nonAdminChildTabCompleter; | ||
private final List<Function<ExecutorData, List<String>>> adminChildTabCompleter; | ||
|
||
public CommandDirector(JavaPlugin plugin, String commandName) { | ||
this.executor = new BlobExecutor(plugin, commandName); | ||
this.nonAdminChildCommands = new ArrayList<>(); | ||
this.adminChildCommands = new ArrayList<>(); | ||
this.nonAdminChildTabCompleter = new ArrayList<>(); | ||
this.adminChildTabCompleter = new ArrayList<>(); | ||
setDefaultCommands(); | ||
setDefaultTabCompleter(); | ||
} | ||
|
||
public BlobExecutor getExecutor() { | ||
return executor; | ||
} | ||
|
||
public void addNonAdminChildCommand(Function<ExecutorData, Boolean> nonAdminChildCommand) { | ||
this.nonAdminChildCommands.add(nonAdminChildCommand); | ||
} | ||
|
||
public void addAdminChildCommand(Function<ExecutorData, Boolean> adminChildCommand) { | ||
this.adminChildCommands.add(adminChildCommand); | ||
} | ||
|
||
public void addNonAdminChildTabCompleter(Function<ExecutorData, List<String>> nonAdminChildTabCompleter) { | ||
this.nonAdminChildTabCompleter.add(nonAdminChildTabCompleter); | ||
} | ||
|
||
public void addAdminChildTabCompleter(Function<ExecutorData, List<String>> adminChildTabCompleter) { | ||
this.adminChildTabCompleter.add(adminChildTabCompleter); | ||
} | ||
|
||
public List<Function<ExecutorData, Boolean>> getAdminChildCommands() { | ||
return adminChildCommands; | ||
} | ||
|
||
public List<Function<ExecutorData, Boolean>> getNonAdminChildCommands() { | ||
return nonAdminChildCommands; | ||
} | ||
|
||
public List<Function<ExecutorData, List<String>>> getAdminChildTabCompleter() { | ||
return adminChildTabCompleter; | ||
} | ||
|
||
public List<Function<ExecutorData, List<String>>> getNonAdminChildTabCompleter() { | ||
return nonAdminChildTabCompleter; | ||
} | ||
|
||
protected void setDefaultCommands() { | ||
executor.setCommand((sender, args) -> { | ||
for (Function<ExecutorData, Boolean> childCommand : nonAdminChildCommands) { | ||
if (childCommand.apply(new ExecutorData(executor, args, sender))) | ||
return true; | ||
} | ||
if (!executor.hasAdminPermission(sender)) | ||
return true; | ||
for (Function<ExecutorData, Boolean> childCommand : adminChildCommands) { | ||
if (childCommand.apply(new ExecutorData(executor, args, sender))) | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
|
||
protected void setDefaultTabCompleter() { | ||
executor.setTabCompleter((sender, args) -> { | ||
List<String> suggestions = new ArrayList<>(); | ||
for (Function<ExecutorData, List<String>> childTabCompleter : nonAdminChildTabCompleter) { | ||
List<String> childTabCompletion = childTabCompleter.apply(new ExecutorData(executor, args, sender)); | ||
if (childTabCompletion != null && !childTabCompletion.isEmpty()) | ||
suggestions.addAll(childTabCompletion); | ||
} | ||
if (!executor.hasAdminPermission(sender, null)) | ||
return suggestions; | ||
for (Function<ExecutorData, List<String>> childTabCompleter : adminChildTabCompleter) { | ||
List<String> childTabCompletion = childTabCompleter.apply(new ExecutorData(executor, args, sender)); | ||
if (childTabCompletion != null && !childTabCompletion.isEmpty()) | ||
suggestions.addAll(childTabCompletion); | ||
} | ||
return suggestions; | ||
}); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/us/mytheria/bloblib/entities/IndependentBuilderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package us.mytheria.bloblib.entities; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import us.mytheria.bloblib.entities.inventory.BlobObjectBuilder; | ||
import us.mytheria.bloblib.managers.ManagerDirector; | ||
|
||
import java.util.UUID; | ||
import java.util.function.Function; | ||
|
||
public class IndependentBuilderManager<T extends BlobObject> extends BuilderManager<T, BlobObjectBuilder<T>> { | ||
|
||
private Function<UUID, BlobObjectBuilder<T>> function; | ||
|
||
public IndependentBuilderManager(ManagerDirector managerDirector, | ||
String fileKey) { | ||
super(managerDirector, fileKey); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public BlobObjectBuilder<T> getOrDefault(UUID uuid) { | ||
BlobObjectBuilder<T> objectBuilder = this.builders.get(uuid); | ||
if (objectBuilder == null) { | ||
objectBuilder = function.apply(uuid); | ||
builders.put(uuid, objectBuilder); | ||
inventories.put(objectBuilder.getInventory(), objectBuilder); | ||
} | ||
return objectBuilder; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public IndependentBuilderManager<T> addBuilder(UUID uuid, BlobObjectBuilder<T> builder) { | ||
super.addBuilder(uuid, builder); | ||
return this; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public IndependentBuilderManager<T> addBuilder(Player player, BlobObjectBuilder<T> builder) { | ||
super.addBuilder(player, builder); | ||
return this; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public IndependentBuilderManager<T> removeBuilder(UUID uuid) { | ||
super.removeBuilder(uuid); | ||
return this; | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public IndependentBuilderManager<T> removeBuilder(Player player) { | ||
super.removeBuilder(player); | ||
return this; | ||
} | ||
|
||
@NotNull | ||
public IndependentBuilderManager<T> setBuilderFunction(Function<UUID, BlobObjectBuilder<T>> function) { | ||
this.function = function; | ||
return this; | ||
} | ||
} |
Oops, something went wrong.