Skip to content

Commit

Permalink
Adds IndependentBuilderManager
Browse files Browse the repository at this point in the history
Adds CommandDirector
Adds BlobObjectBuilder
  • Loading branch information
anjoismysign committed Dec 23, 2023
1 parent 86f80e2 commit 2b698a5
Show file tree
Hide file tree
Showing 13 changed files with 935 additions and 744 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.41</version>
<version>1.697.47</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.41</version>
<version>1.697.47</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.41</version>
<version>1.697.47</version>
<packaging>pom</packaging>

<properties>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/us/mytheria/bloblib/BlobLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class BlobLib extends JavaPlugin {
private SerializationLib serializationLib;
private InventoryTrackerManager inventoryTrackerManager;
private TranslatableManager translatableManager;
private TranslatablePH translatablePH;

private LocalizableDataAssetManager<TranslatableItem> translatableItemManager;
private DataAssetManager<TagSet> tagSetManager;
Expand Down
145 changes: 145 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/BuilderManager.java
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 src/main/java/us/mytheria/bloblib/entities/CommandDirector.java
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;
});
}
}
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;
}
}
Loading

0 comments on commit 2b698a5

Please sign in to comment.