Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions paper/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
id 'java'
id 'com.gradleup.shadow' version '8.3.3'
id 'org.jetbrains.kotlin.jvm' version '2.0.21'
id 'io.papermc.paperweight.userdev' version '2.0.0-beta.14'
id 'org.jetbrains.kotlin.jvm' version '2.1.20'
id "io.papermc.paperweight.userdev" version "2.0.0-beta.14"
id 'maven-publish'
}

Expand Down Expand Up @@ -91,6 +91,9 @@ dependencies {
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.36'
annotationProcessor 'org.projectlombok:lombok:1.18.36'

// Json?
implementation 'org.json:json:20250107'
}

tasks.withType(JavaCompile).configureEach {
Expand Down
363 changes: 130 additions & 233 deletions paper/src/main/java/com/al3x/housing2/Action/Action.java

Large diffs are not rendered by default.

179 changes: 10 additions & 169 deletions paper/src/main/java/com/al3x/housing2/Action/ActionEditor.java
Original file line number Diff line number Diff line change
@@ -1,191 +1,32 @@
package com.al3x.housing2.Action;

import com.al3x.housing2.Utils.ItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Setter
@Getter
public class ActionEditor {
private int rows = 4;
private String title = "Action Settings";
private List<ActionItem> items = new ArrayList<>();
private List<ActionProperty<?>> properties = new ArrayList<>();

public ActionEditor() {
}

public ActionEditor(int rows, String title, List<ActionItem> items) {
public ActionEditor(int rows, String title, List<ActionProperty<?>> properties) {
this.rows = rows;
this.title = title;
this.items = items;
this.properties = properties.stream().filter(actionProperty -> actionProperty.getVisible().apply()).collect(Collectors.toList());
}

public ActionEditor(int rows, String title, ActionItem... items) {
public ActionEditor(int rows, String title, ActionProperty<?>... properties) {
this.rows = rows;
this.title = title;
this.items = Arrays.asList(items);
this.properties = Arrays.stream(properties).filter(actionProperty -> actionProperty.getVisible().apply()).collect(Collectors.toList());;
}

public int getRows() {
return rows;
}

public void setRows(int rows) {
this.rows = rows;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public List<ActionItem> getItems() {
return items;
}

public void setItems(List<ActionItem> items) {
this.items = items;
}

public static class ActionItem {
private ItemBuilder builder;
private ActionType type;
private int slot = -1;
private double min = 0;
private double max = Double.MAX_VALUE;
private Enum[] enumClass;
private Material enumMaterial;
private String varName;
private BiFunction<InventoryClickEvent, Object, Boolean> customRunnable;

public ActionItem(String varName, ItemBuilder builder, ActionType type) {
this.varName = varName;
this.builder = builder;
this.type = type;

if (type == ActionType.ENUM) {
Bukkit.getLogger().warning(varName + " is an enum type, but no enum class was provided.");
}
}

public ActionItem(String varName, ItemBuilder builder, ActionType type, BiFunction<InventoryClickEvent, Object, Boolean> runnable) {
this.varName = varName;
this.builder = builder;
this.type = type;
this.customRunnable = runnable;

if (type == ActionType.ENUM) {
Bukkit.getLogger().warning(varName + " is an enum type, but no enum class was provided.");
}
}

public ActionItem(ItemBuilder builder, ActionType type, int slot, BiFunction<InventoryClickEvent, Object, Boolean> consumer) {
this.builder = builder;
this.type = type;
this.slot = slot;
this.customRunnable = consumer;

if (type != ActionType.CUSTOM) {
Bukkit.getLogger().warning("Slot provided for non-custom action item.");
}
}

public ActionItem(String varName, ItemBuilder builder, ActionType type, double min, double max) {
this.varName = varName;
this.builder = builder;
this.type = type;
this.min = min;
this.max = max;

if (type != ActionType.DOUBLE && type != ActionType.INT) {
Bukkit.getLogger().warning(varName + " is not a number type, but min and max values were provided.");
}
}

public ActionItem(String varName, ItemBuilder builder, ActionType type, Enum[] enumClass, Material enumMaterial) {
this.varName = varName;
this.builder = builder;
this.type = type;
this.enumClass = enumClass;
this.enumMaterial = enumMaterial;
}

public ActionItem(String varName, ItemBuilder builder, ActionType type, Enum[] enumClass, Material enumMaterial, BiFunction<InventoryClickEvent, Object, Boolean> customRunnable) {
this.varName = varName;
this.builder = builder;
this.type = type;
this.enumClass = enumClass;
this.enumMaterial = enumMaterial;
this.customRunnable = customRunnable;
}

public ActionItem(String varName, ItemBuilder builder, BiFunction<InventoryClickEvent, Object, Boolean> customRunnable) {
this.varName = varName;
this.builder = builder;
this.customRunnable = customRunnable;
}

public ItemBuilder getBuilder() {
return builder;
}

public ActionType getType() {
return type;
}

public Enum[] getEnumClass() {
return enumClass;
}

public int getSlot() {
return slot;
}

public Material getEnumMaterial() {
return enumMaterial;
}

public String getVarName() {
return varName;
}

public BiFunction<InventoryClickEvent, Object, Boolean> getCustomRunnable() {
return customRunnable;
}

public double getMin() {
return min;
}

public double getMax() {
return max;
}

public void setCustomRunnable(BiFunction<InventoryClickEvent, Object, Boolean> customRunnable) {
this.customRunnable = customRunnable;
}

public void setType(ActionType type) {
this.type = type;
}

public void setBuilder(ItemBuilder builder) {
this.builder = builder;
}

public enum ActionType {
STRING, INT, DOUBLE, BOOLEAN, ENUM, ITEM, NPC, HOUSE, REGION, ACTION, ACTION_SETTING, FUNCTION, GROUP, TEAM, LAYOUT, MENU, CONDITION, CUSTOM
}
}


}
31 changes: 13 additions & 18 deletions paper/src/main/java/com/al3x/housing2/Action/ActionEnum.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.al3x.housing2.Action;

import com.al3x.housing2.Action.Actions.*;
import com.al3x.housing2.Action.Actions.AttackEntityAction;
import com.al3x.housing2.Instances.HousingWorld;
import lombok.Getter;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

@Getter
public enum ActionEnum {
CONDITIONAL("Conditional Action", ConditionalAction.class),
CANCEL("Cancel Action", CancelAction.class),
Expand Down Expand Up @@ -43,7 +47,7 @@ public enum ActionEnum {
PARTICLE("Particle Action", ParticleAction.class),
CHANGE_NPC_NAVIGATION("Change Npc Navigation Action", NpcPathAction.class),
CHANGE_PLAYER_DISPLAYNAME("Change Player Display Name Action", ChangePlayerDisplayNameAction.class),
CHANGE_NAMETAG("NameTag Action", NameTagAction.class),
CHANGE_PLAYER_NAMETAG("NameTag Action", NameTagAction.class),
REPEAT("Repeat Action", RepeatAction.class),
CLEAR_BOSSBAR("Clear Bossbars Action", ClearBossbarAction.class),
CLEAR_PLAYERSTATS("Clear Player Stats Action", ClearPlayerStatsAction.class),
Expand Down Expand Up @@ -75,28 +79,19 @@ public enum ActionEnum {
SPAWN_GHOST_BLOCK("Spawn Ghost Block", SpawnGhostBlock.class),
SET_PLAYER_SLOT("Set Player Slot Action", SetPlayerSlotAction.class);
// Add new actions here
// Name of the action and the class that has the name of the action need to be the exact same
;
private String name;
private Class<? extends Action> action;
private final String id;
private final Class<? extends Action> action;

ActionEnum(String name, Class<? extends Action> action) {
ActionEnum(String id, Class<? extends Action> action) {
this.id = id;
this.action = action;
this.name = name;
}

public Class<? extends Action> getAction() {
return action;
}

public String getName() {
return name;
}

public Action getActionInstance(HashMap<String, Object> data, String comment) {
public Action getActionInstance(HashMap<String, Object> data, String comment, HousingWorld house) {
try {
Action action = this.action.getDeclaredConstructor().newInstance();
action.fromData(data, this.action);
action.fromData(data, house);
action.setComment(comment);
return action;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Expand All @@ -114,9 +109,9 @@ public Action getActionInstance() {
return null;
}

public static ActionEnum getActionByName(String name) {
public static ActionEnum getActionById(String id) {
for (ActionEnum action : ActionEnum.values()) {
if (action.name.equals(name)) {
if (action.getId().equals(id)) {
return action;
}
}
Expand Down
20 changes: 6 additions & 14 deletions paper/src/main/java/com/al3x/housing2/Action/ActionExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@
import com.al3x.housing2.Action.Actions.ContinueAction;
import com.al3x.housing2.Action.Actions.ExitAction;
import com.al3x.housing2.Action.Actions.PauseAction;
import com.al3x.housing2.Action.Properties.NumberProperty;
import com.al3x.housing2.Events.CancellableEvent;
import com.al3x.housing2.Instances.HousingWorld;
import com.al3x.housing2.Instances.Stat;
import com.al3x.housing2.Main;
import com.al3x.housing2.Placeholders.custom.Placeholder;
import com.al3x.housing2.Utils.NumberUtilsKt;
import de.maxhenkel.voicechat.api.events.Event;
import lombok.Setter;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.scheduler.BukkitScheduler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static com.al3x.housing2.Action.OutputType.*;
Expand Down Expand Up @@ -111,18 +107,14 @@ public OutputType execute(Entity entity, Player player, HousingWorld house, Canc
Action action = queue.removeFirst();

int global = Main.getInstance().getConfig().getInt("globalLimits", 2000);
int actionLimit = Main.getInstance().getConfig().getInt("actionLimits." + action.name.replace(" ", "_").toLowerCase(), global);
if (limits.containsKey(action.name) && limits.get(action.name) >= actionLimit) {
int actionLimit = Main.getInstance().getConfig().getInt("actionLimits." + action.getId().replace(" ", "_").toLowerCase(), global);
if (limits.containsKey(action.getName()) && limits.get(action.getName()) >= actionLimit) {
return EXIT;
}
limits.put(action.name, limits.getOrDefault(action.name, 0) + 1);
limits.put(action.getId(), limits.getOrDefault(action.getId(), 0) + 1);

if (action instanceof PauseAction pauseAction) {
String dur = Placeholder.handlePlaceholders(pauseAction.getDuration(), house, player);
if (!NumberUtilsKt.isInt(dur)) {
continue;
}
double duration = Integer.parseInt(dur);
double duration = pauseAction.getProperty("duration", NumberProperty.class).parsedValue(house, player);
scheduler.runTaskLater(Main.getInstance(), () -> {
execute(entity, player, house, event);
}, (long) duration);
Expand Down Expand Up @@ -170,7 +162,7 @@ public OutputType execute(Entity entity, Player player, HousingWorld house, Canc
npcAction.npcExecute(player, npc, house, event, this);
}
} catch (Exception e) {
player.sendMessage("An error occurred while executing the action: " + action.name + " in the context: " + context);
player.sendMessage("An error occurred while executing the action: " + action.getName() + " in the context: " + context);
//take error and put it in the hover event
e.printStackTrace();
}
Expand Down
Loading