Skip to content

Commit

Permalink
Made entity settings work and expanded possibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
FireInstall committed Jan 24, 2024
1 parent f2a077a commit eb5825c
Show file tree
Hide file tree
Showing 72 changed files with 1,063 additions and 541 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package au.com.mineauz.minigames.config;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class ConfigSerializableBridge<T> {
private final @NotNull T object;

public ConfigSerializableBridge(@NotNull T serializable) throws IllegalArgumentException {
if (!(serializable instanceof ConfigurationSerializable) && !(serializable instanceof Serializable)) {
throw new IllegalArgumentException("Object was nighter a Serializable nor a ConfigurationSerializable");
}

this.object = serializable;
}

public Object serialize() {
if (object instanceof ConfigurationSerializable configurationSerializable) {
Map<String, Object> map = new LinkedHashMap<>();
// add key for deserialization at top
map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(configurationSerializable.getClass()));
map.putAll(((ConfigurationSerializable) object).serialize());
return map;
} else {
return object;
}
}

public static @Nullable ConfigSerializableBridge<?> deserialize(Object object) {
if (object instanceof ConfigurationSerializable serializable) { // bukkit already did the work for us
return new ConfigSerializableBridge<>(serializable);
} else if (object instanceof ConfigurationSection configSection) { // configs are weird.
Map<String, Object> stringMap = configSection.getValues(false);
try {
ConfigurationSerializable configurationSerializable = ConfigurationSerialization.deserializeObject(stringMap);
if (configurationSerializable != null) {
return new ConfigSerializableBridge<>(configurationSerializable);
}
} catch (IllegalArgumentException ignored) {
}

} else if (object instanceof Map<?, ?> objMap) {
Map<String, Object> stringMap = new HashMap<>();

for (Map.Entry<?, ?> entry : objMap.entrySet()) {
stringMap.put(entry.getKey().toString(), entry.getValue());
}

try {
ConfigurationSerializable configurationSerializable = ConfigurationSerialization.deserializeObject(stringMap);
if (configurationSerializable != null) {
return new ConfigSerializableBridge<>(configurationSerializable);
}
} catch (IllegalArgumentException ignored) {
}
}

if (object instanceof Serializable serializable) {
return new ConfigSerializableBridge<>(serializable);
} else {
return null;
//throw new IllegalArgumentException("Object was nighter a Serializable nor a ConfigurationSerializable");
}
}

public @NotNull T getObject() {
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public String getValue() {
public void setValue(String value) {
setFlag(value);
}


});
}

Expand All @@ -61,8 +59,6 @@ public String getValue() {
public void setValue(String value) {
setFlag(value);
}


});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.com.mineauz.minigames.menu;

import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -32,17 +33,31 @@ public MenuItem(String name, List<String> description, Material displayItem) {
this.displayItem.setItemMeta(meta);
}

public List<String> getDescription() {
@Deprecated(forRemoval = true)
public List<String> getDescriptionStr() {
return displayItem.getItemMeta().getLore();
}

public void setDescription(List<String> description) {

public List<Component> getDescriptionComp() {
return displayItem.getItemMeta().lore();
}

@Deprecated(forRemoval = true)
public void setDescriptionStr(List<String> description) {
ItemMeta meta = displayItem.getItemMeta();

meta.setLore(description);
displayItem.setItemMeta(meta);
}

public void setDescriptionComp(List<Component> description) {
ItemMeta meta = displayItem.getItemMeta();

meta.lore(description);
displayItem.setItemMeta(meta);
}

public String getName() {
return displayItem.getItemMeta().getDisplayName();
}
Expand All @@ -53,7 +68,7 @@ public ItemStack getItem() {

public void setItem(ItemStack item) {
if (item == null) {
Bukkit.getLogger().fine("Item Stack was null on: " + this.getDescription().toString());
Bukkit.getLogger().fine("Item Stack was null on: " + this.getDescriptionStr().toString());
return;
}
ItemMeta ometa = displayItem.getItemMeta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MenuItemAddWhitelistBlock extends MenuItem {

public MenuItemAddWhitelistBlock(String name, List<Material> whitelist) {
super(name, MenuUtility.getCreateMaterial());
setDescription(List.of("Left Click with item to", "add to whitelist/blacklist", "Click without item to", "manually add item."));
setDescriptionStr(List.of("Left Click with item to", "add to whitelist/blacklist", "Click without item to", "manually add item."));
this.whitelist = whitelist;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
import java.util.List;

public class MenuItemBlockData extends MenuItem {
private Callback<BlockData> data;
private Callback<BlockData> dataCallback;

public MenuItemBlockData(String name, Material displayItem) {
super(name, displayItem);
data.setValue(displayItem.createBlockData());
setDescription(createDescription(data.getValue()));
dataCallback.setValue(displayItem.createBlockData());
setDescriptionStr(createDescription(dataCallback.getValue()));
}

public MenuItemBlockData(String name, Material displayItem, Callback<BlockData> callback) {
super(name, displayItem);
this.data = callback;
setDescription(createDescription(data.getValue()));
this.dataCallback = callback;
setDescriptionStr(createDescription(dataCallback.getValue()));
}

@Override
public void update() {
setDescription(createDescription(this.data.getValue()));
setDescriptionStr(createDescription(this.dataCallback.getValue()));
}

/**
Expand Down Expand Up @@ -61,7 +61,12 @@ private List<String> createDescription(BlockData data) {
public ItemStack onClickWithItem(@Nullable ItemStack item) {
try {
BlockData data = item.getType().createBlockData();
this.data.setValue(data);
this.dataCallback.setValue(data);

// update the display item
ItemStack stackUpdate = getItem();
stackUpdate.setType(item.getType());
setItem(stackUpdate);
} catch (IllegalArgumentException | NullPointerException e) {
String name = "unknown";
if (item != null) {
Expand All @@ -77,8 +82,16 @@ public void checkValidEntry(String entry) {
String err = "No MgBlockData detected";
try {
BlockData d = Bukkit.createBlockData(entry);
data.setValue(d);
setDescription(createDescription(data.getValue()));
dataCallback.setValue(d);

// update the display item
setDescriptionStr(createDescription(dataCallback.getValue()));
if (d.getMaterial().isItem()) {
ItemStack stackUpdate = getItem();
stackUpdate.setType(d.getMaterial());
setItem(stackUpdate);
}

getContainer().cancelReopenTimer();
getContainer().displayMenu(getContainer().getViewer());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public void updateDescription() {
} else {
col = ChatColor.RED + "false";
}
if (getDescription() != null) {
description = getDescription();
String desc = ChatColor.stripColor(getDescription().get(0));
if (getDescriptionStr() != null) {
description = getDescriptionStr();
String desc = ChatColor.stripColor(getDescriptionStr().get(0));

if (desc.matches("true|false"))
description.set(0, col);
Expand All @@ -43,7 +43,7 @@ public void updateDescription() {
description.add(col);
}

setDescription(description);
setDescriptionStr(description);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package au.com.mineauz.minigames.menu;

import au.com.mineauz.minigames.MinigameMessageType;
import au.com.mineauz.minigames.objects.MinigamePlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;

public class MenuItemComponent extends MenuItem {
protected Callback<String> stringCallback; // todo I'm not happy having String as callback here. I need it since the SerializeableBridge can't use Components, but this doesn't feel right.
private boolean allowNull = false;

public MenuItemComponent(String name, Material displayItem, Callback<String> stringCallback) {
super(name, displayItem);
this.stringCallback = stringCallback;
updateDescription();
}

public MenuItemComponent(String name, List<String> description, Material displayItem, Callback<String> stringCallback) {
super(name, description, displayItem);
this.stringCallback = stringCallback;
updateDescription();
}

public void setAllowNull(boolean allow) {
allowNull = allow;
}

public void updateDescription() {
List<Component> description;
String setting = stringCallback.getValue();
if (setting == null) {
setting = "<red>Not Set</red>";

}

Component settingComp = MiniMessage.miniMessage().deserialize(setting);
//todo find a way to effective limit the length without messing with styles
//if (setting.length() > 20) {
// setting = setting.substring(0, 17) + "...";
//}

description = getDescriptionComp();
if (description != null) {
//todo find a way to not overwrite other descriptions
//Component desc = description.get(0);

// if (desc.color() == NamedTextColor.GREEN) {
description.set(0, settingComp);
// } else {
// description.add(0, setting.color(NamedTextColor.GREEN));
// }
} else {
description = new ArrayList<>();
description.add(settingComp);
}

setDescriptionComp(description);
}

@Override
public ItemStack onDoubleClick() {
MinigamePlayer ply = getContainer().getViewer();
ply.setNoClose(true);
ply.getPlayer().closeInventory();
ply.sendMessage("Enter mini message value into chat for " + getName() + ", the menu will automatically reopen in 20s if nothing is entered.", MinigameMessageType.INFO);
if (allowNull) {
ply.sendInfoMessage("Enter \"null\" to remove the string value");
}
ply.setManualEntry(this);
getContainer().startReopenTimer(20);

return null;
}

@Override
public void checkValidEntry(String entry) {
if (entry.equals("null") && allowNull) {
stringCallback.setValue(null);
} else {
stringCallback.setValue(entry);
}

updateDescription();
getContainer().cancelReopenTimer();
getContainer().displayMenu(getContainer().getViewer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public void setFormat(DecimalFormat format) {

public void updateDescription() {
List<String> description;
if (getDescription() != null) {
description = getDescription();
String desc = ChatColor.stripColor(getDescription().get(0));
if (getDescriptionStr() != null) {
description = getDescriptionStr();
String desc = ChatColor.stripColor(getDescriptionStr().get(0));

if (desc.matches("-?[0-9]+(.[0-9]+)?"))
if (desc.matches("-?[0-9]+(?:.[0-9]+)?")) {
description.set(0, ChatColor.GREEN + form.format(value.getValue()));
else if (value.getValue().isInfinite()) {
} else if (value.getValue().isInfinite()) {
description.add(0, ChatColor.GREEN + "INFINITE");
} else {
description.add(0, ChatColor.GREEN + form.format(value.getValue()));
Expand All @@ -67,13 +67,13 @@ else if (value.getValue().isInfinite()) {
}
}

setDescription(description);
setDescriptionStr(description);
}

@Override
public ItemStack onClick() {
if (max == null || value.getValue() < max)
value.setValue(Double.valueOf(form.format(value.getValue() + lowerInc)));
value.setValue(value.getValue() + lowerInc);
if (max != null && value.getValue() > max)
value.setValue(max);
updateDescription();
Expand All @@ -83,7 +83,7 @@ public ItemStack onClick() {
@Override
public ItemStack onRightClick() {
if (min == null || value.getValue() > min)
value.setValue(Double.valueOf(form.format(value.getValue() - lowerInc)));
value.setValue(value.getValue() - lowerInc);
if (min != null && value.getValue() < min)
value.setValue(min);
updateDescription();
Expand All @@ -93,7 +93,7 @@ public ItemStack onRightClick() {
@Override
public ItemStack onShiftClick() {
if (max == null || value.getValue() < max)
value.setValue(Double.valueOf(form.format(value.getValue() + upperInc)));
value.setValue(value.getValue() + upperInc);
if (max != null && value.getValue() > max)
value.setValue(max);
updateDescription();
Expand All @@ -103,7 +103,7 @@ public ItemStack onShiftClick() {
@Override
public ItemStack onShiftRightClick() {
if (min == null || value.getValue() > min)
value.setValue(Double.valueOf(form.format(value.getValue() - upperInc)));
value.setValue(value.getValue() - upperInc);
if (min != null && value.getValue() < min)
value.setValue(min);
updateDescription();
Expand Down
Loading

0 comments on commit eb5825c

Please sign in to comment.