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
2 changes: 1 addition & 1 deletion API/src/main/java/fr/maxlego08/menu/api/ItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface ItemManager {

void unloadListeners();

void registerMechanicFactory(MechanicFactory factory);
void registerMechanicFactory(MechanicFactory<?> factory);

void giveItem(Player player, String itemId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class Config {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.maxlego08.menu.api.enums;

import java.util.ArrayList;
import fr.maxlego08.menu.zcore.logger.Logger;

import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -51,7 +52,7 @@ public static PlaceholderAction from(String string) {
}
}
}
System.err.println("Impossible to find the " + string + " action for placeholder");
Logger.info("Impossible to find the " + string + " action for placeholder", Logger.LogType.ERROR);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fr.maxlego08.menu.api.event.events;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
* Called when zMenu custom items are loaded or reloaded.
* This event is fired after all items and their mechanics have been processed.
*/
public class ZMenuItemsLoad extends Event {

private static final HandlerList HANDLERS = new HandlerList();
private final Set<String> loadedItemIds;
private final boolean isReload;

/**
* Constructor for ZMenuItemsLoad event.
*
* @param loadedItemIds Set of all loaded item IDs
* @param isReload true if this is a reload, false if initial load
*/
public ZMenuItemsLoad(@NotNull Set<String> loadedItemIds, boolean isReload) {
this.loadedItemIds = loadedItemIds;
this.isReload = isReload;
}

/**
* Gets the set of all loaded item IDs.
*
* @return Set of item IDs that were loaded
*/
@NotNull
public Set<String> getLoadedItemIds() {
return loadedItemIds;
}

/**
* Checks if this event was triggered by a reload.
*
* @return true if this is a reload, false if initial load
*/
public boolean isReload() {
return isReload;
}

@Override
public @NotNull HandlerList getHandlers() {
return HANDLERS;
}

@NotNull
public static HandlerList getHandlerList() {
return HANDLERS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import org.bukkit.configuration.ConfigurationSection;

public abstract class Mechanic {
public abstract class Mechanic<T extends MechanicFactory<?>> {
private final ConfigurationSection mechanicSection;
private final MechanicFactory mechanicFactory;
private final T mechanicFactory;
private final String itemId;

public Mechanic(final String itemId, final MechanicFactory mechanicFactory, final ConfigurationSection mechanicSection) {
public Mechanic(final String itemId, final T mechanicFactory, final ConfigurationSection mechanicSection) {
this.mechanicFactory = mechanicFactory;
this.mechanicSection = mechanicSection;
this.itemId = itemId;
Expand All @@ -17,7 +17,7 @@ public ConfigurationSection getMechanicSection() {
return mechanicSection;
}

public MechanicFactory getMechanicFactory() {
public T getMechanicFactory() {
return mechanicFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,33 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public abstract class MechanicFactory {
public abstract class MechanicFactory<T extends Mechanic> {
private final MenuPlugin plugin;
private final ItemManager itemManager;
private final String mechanicId;
private final Map<String, Mechanic> mechanicByItemId = new HashMap<>();
private final Map<String, T> mechanicByItemId = new HashMap<>();

public MechanicFactory(MenuPlugin plugin, String mechanicId) {
this.plugin = plugin;
this.mechanicId = mechanicId;
this.itemManager = plugin.getItemManager();
}

public abstract Mechanic parse(final MenuPlugin plugin, final String itemId, final ConfigurationSection mechanicSection, YamlConfiguration configurationFile, File file, String path);
public abstract T parse(final MenuPlugin plugin, final String itemId, final ConfigurationSection mechanicSection, YamlConfiguration configurationFile, File file, String path);

public Mechanic getMechanic(String itemId){
public @Nullable T getMechanic(String itemId){
return mechanicByItemId.get(itemId);
};
}

public void addToImplemented(Mechanic mechanic) {
public void addToImplemented(T mechanic) {
mechanicByItemId.put(mechanic.getItemId(), mechanic);
}

Expand All @@ -47,7 +48,7 @@ public String getMechanicId() {
return mechanicId;
}

public Set<Map.Entry<String, Mechanic>> getAllMechanics(){
public Set<Map.Entry<String, T>> getAllMechanics(){
return mechanicByItemId.entrySet();
}

Expand Down
21 changes: 20 additions & 1 deletion API/src/main/java/fr/maxlego08/menu/api/requirement/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class Action {
*/
private int delay;
private float chance;
private final List<Action> denyChanceActions = new ArrayList<>();

/**
* Executes the action for the specified player.
Expand All @@ -33,7 +34,12 @@ public abstract class Action {

public void preExecute(Player player, Button button, InventoryEngine inventoryEngine, Placeholders placeholders) {
placeholders.register("player", player.getName());
if (chance < 100 && Math.random() > (chance / 100.0f)) return;
if (chance < 100 && Math.random() > (chance / 100.0f)){
for (Action denyChanceAction : denyChanceActions){
denyChanceAction.preExecute(player, button, inventoryEngine, placeholders);
}
return;
};
if (delay == 0) execute(player, button, inventoryEngine, placeholders);
else {
inventoryEngine.getPlugin().getScheduler().runAtEntityLater(player, () -> execute(player, button, inventoryEngine, placeholders), this.delay);
Expand All @@ -48,6 +54,7 @@ public void setDelay(int delay) {
this.delay = delay;
}

@SuppressWarnings("unused")
public float getChance() {return chance;}

public void setChance(float chance) {
Expand All @@ -60,6 +67,18 @@ public void setChance(float chance) {
this.chance = chance;
}

@SuppressWarnings("unused")
public List<Action> getDenyChanceActions() {
return denyChanceActions;
}

public void setDenyChanceActions(List<Action> denyChanceActions) {
this.denyChanceActions.clear();
if (denyChanceActions != null) {
this.denyChanceActions.addAll(denyChanceActions);
}
}

/**
* Parses and flattens a list of commands by splitting each command by newline, replacing a
* placeholder with the player's name, and returning a flattened list of processed commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.maxlego08.menu.api.players.Data;
import fr.maxlego08.menu.api.players.DataManager;
import fr.maxlego08.menu.api.utils.Placeholders;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -36,21 +37,31 @@ public interface ActionPlayerData {
*
* @return The expiration time in seconds.
*/
long getSeconds();
String getSeconds();

/**
* Converts the action into player data.
* <p>
* DEPRECATED: Use {@link #toData(OfflinePlayer, Placeholders)} instead.
*
* @param player The player for whom the data is created.
* @return The {@link Data}.
*/
@Deprecated(since = "1.1.0.6")
Data toData(OfflinePlayer player);

Data toData(OfflinePlayer player, Placeholders placeholders);

/**
* Executes the action when the player clicks.
* <p>
* DEPRECATED: Use {@link #execute(Player, DataManager, Placeholders)} instead.
*
* @param player The player who executes the action.
* @param dataManager The {@link DataManager}.
*/
@Deprecated(since = "1.1.0.6")
void execute(Player player, DataManager dataManager);

void execute(Player player, DataManager dataManager, Placeholders placeholders);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.maxlego08.menu.api.utils;

import fr.maxlego08.menu.zcore.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -115,7 +117,7 @@ public String parse(String string, String key, String value) {
}
} catch (Exception exception) {
exception.printStackTrace();
System.err.println("Error with placeholder key " + key + " !");
Logger.info("Error with placeholder key " + key + " !", Logger.LogType.ERROR);
}
return string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,13 @@ public String toString() {
"map=" + map +
'}';
}

@SuppressWarnings("unchecked")
public List<Map<String,Object>> getMapList(String key) {
try {
return (List<Map<String, Object>>) map.getOrDefault(key, new ArrayList<Map<String, Object>>());
} catch (ClassCastException e){
return new ArrayList<>();
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/fr/maxlego08/menu/ZButtonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ public List<Action> loadActions(List<Map<String, Object>> elements, String path,
if (action != null) {
action.setDelay(accessor.getInt("delay", 0));
action.setChance(accessor.getFloat("chance", 100));
List<Map<String, Object>> denyChanceAction = accessor.getMapList("deny-chance-actions");
if (!denyChanceAction.isEmpty()) {
List<Action> denyActions = loadActions(denyChanceAction, path + ".deny-chance-actions", file);
if (!denyActions.isEmpty()) {
action.setDenyChanceActions(denyActions);
}
}
actions.add(action);
}
} else {
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/fr/maxlego08/menu/ZItemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.maxlego08.menu.api.MenuItemStack;
import fr.maxlego08.menu.api.MenuPlugin;
import fr.maxlego08.menu.api.configuration.Config;
import fr.maxlego08.menu.api.event.events.ZMenuItemsLoad;
import fr.maxlego08.menu.api.mechanic.MechanicFactory;
import fr.maxlego08.menu.api.mechanic.MechanicListener;
import fr.maxlego08.menu.item.CustomItemData;
Expand All @@ -17,6 +18,7 @@
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;

Expand All @@ -34,10 +36,12 @@ public class ZItemManager implements ItemManager{
private final MenuPlugin menuPlugin;

private final Map<String, MechanicListener> mechanicListeners = new HashMap<>();
private final Map<String, MechanicFactory> mechanicFactories = new HashMap<>();
private final Map<String, MechanicFactory<?>> mechanicFactories = new HashMap<>();

private final Map<String, CustomItemData> customItems = new HashMap<>();

private boolean isFirstLoad = true;

public ZItemManager(ZMenuPlugin menuPlugin) {
this.itemIdKey = new NamespacedKey(menuPlugin, "item-id");
this.ownerKey = new NamespacedKey(menuPlugin, "owner");
Expand Down Expand Up @@ -66,6 +70,11 @@ public void loadCustomItems() {
}
try (Stream<Path> stream = Files.walk(Paths.get(itemsFolder.getPath()))) {
stream.skip(1).map(Path::toFile).filter(File::isFile).filter(e -> e.getName().endsWith(".yml")).forEach(this::loadCustomItem);
ZMenuItemsLoad event = new ZMenuItemsLoad(new HashSet<>(customItems.keySet()), !isFirstLoad);
menuPlugin.getServer().getPluginManager().callEvent(event);
if (isFirstLoad) {
isFirstLoad = false;
}
} catch (IOException exception) {
if (Config.enableDebug){
Logger.info("Error while loading items: " + exception.getMessage(), Logger.LogType.ERROR);
Expand All @@ -86,7 +95,7 @@ public void loadCustomItem(File file) {
if (mechanicSection != null) {
path += "mechanics.";
for (String mechanicId : mechanicSection.getKeys(false)) {
MechanicFactory factory = mechanicFactories.get(mechanicId);
MechanicFactory<?> factory = mechanicFactories.get(mechanicId);
if (factory != null) {
factory.parse(this.menuPlugin, itemId, mechanicSection.getConfigurationSection(mechanicId), config, file, path + mechanicId + ".");
mechanicIds.add(mechanicId);
Expand All @@ -108,7 +117,7 @@ public void loadCustomItem(File file) {
@Override
public void reloadCustomItems() {
customItems.clear();
for (MechanicFactory factory : mechanicFactories.values()) {
for (MechanicFactory<?> factory : mechanicFactories.values()) {
factory.clearMechanics();
}
this.loadCustomItems();
Expand Down Expand Up @@ -158,7 +167,7 @@ public void unloadListeners() {
}

@Override
public void registerMechanicFactory(MechanicFactory factory) {
public void registerMechanicFactory(MechanicFactory<?> factory) {
if (mechanicFactories.containsKey(factory.getMechanicId())) {
Logger.info("MechanicFactory " + factory.getMechanicId() + " is already registered.", Logger.LogType.WARNING);
return;
Expand All @@ -177,9 +186,12 @@ public void giveItem(Player player, String itemId) {
MenuItemStack menuItemStack = itemData.menuItemStack();
ItemStack itemStack = menuItemStack.build(player);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, itemId);
itemMeta.getPersistentDataContainer().set(ownerKey, PersistentDataType.STRING, player.getUniqueId().toString());
itemStack.setItemMeta(itemMeta);
if (itemMeta != null) {
PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
persistentDataContainer.set(itemIdKey, PersistentDataType.STRING, itemId);
persistentDataContainer.set(ownerKey, PersistentDataType.STRING, player.getUniqueId().toString());
itemStack.setItemMeta(itemMeta);
}

boolean shouldCancel = false;
for (String mechanicId : itemData.mechanicIds()) {
Expand Down
Loading