forked from AddstarMC/Minigames
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made entity settings work and expanded possibilities
- Loading branch information
1 parent
f2a077a
commit eb5825c
Showing
72 changed files
with
1,063 additions
and
541 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Minigames/src/main/java/au/com/mineauz/minigames/config/ConfigSerializableBridge.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,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; | ||
} | ||
} |
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
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
92 changes: 92 additions & 0 deletions
92
Minigames/src/main/java/au/com/mineauz/minigames/menu/MenuItemComponent.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,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()); | ||
} | ||
} |
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
Oops, something went wrong.