Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make visual effects serializable #7114

Open
wants to merge 4 commits into
base: dev/feature
Choose a base branch
from
Open
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
66 changes: 47 additions & 19 deletions src/main/java/ch/njol/skript/util/visual/VisualEffectType.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,65 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.util.visual;

import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.log.BlockingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.variables.Variables;
import ch.njol.yggdrasil.Fields;
import ch.njol.yggdrasil.YggdrasilSerializable;
import ch.njol.yggdrasil.YggdrasilSerializer;
import org.bukkit.Effect;
import org.bukkit.EntityEffect;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.jetbrains.annotations.Nullable;

import java.io.NotSerializableException;
import java.io.StreamCorruptedException;
import java.util.Objects;
import java.util.function.BiFunction;

public class VisualEffectType {
public class VisualEffectType implements YggdrasilSerializable {

static {
Variables.yggdrasil.registerClassResolver(new YggdrasilSerializer<VisualEffectType>() {
@Override
public @Nullable Class<? extends VisualEffectType> getClass(String id) {
return id.equals("VisualEffectType") ? VisualEffectType.class : null;
}

@Override
public Fields serialize(VisualEffectType visualEffectType) {
Fields fields = new Fields();
fields.putObject("id", visualEffectType.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing a quick look thru the VisualEffectType code, I see this regarding IDs

public String getId() {
	return effect.getDeclaringClass().getSimpleName() + "." + effect.name();
}

The issue here is the ID is the class name and enum name.
Problem comes from the fact Mojang has changed particle names several times in the last few versions.
And as of I believe 1.21 Bukkit has changed a whole bunch of the enums.
That said, this is not version safe.

return fields;
}

@Override
public @Nullable <E extends VisualEffectType> E newInstance(Class<E> clazz) {
return null;
}

@Override
public boolean canBeInstantiated(Class<? extends VisualEffectType> clazz) {
return false;
}

@Override
public void deserialize(VisualEffectType visualEffectType, Fields fields) { }

@Override
@SuppressWarnings("unchecked")
public <E extends VisualEffectType> E deserialize(Class<E> clazz, Fields fields) throws StreamCorruptedException, NotSerializableException {
return (E) VisualEffects.get(fields.getObject("id", String.class));
}

@Override
public @Nullable String getID(Class<?> clazz) {
return clazz.equals(VisualEffectType.class) ? "VisualEffectType" : null;
}
});
}

private static final String LANGUAGE_NODE = "visual effects";

Expand Down
27 changes: 8 additions & 19 deletions src/main/java/ch/njol/skript/util/visual/VisualEffects.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.util.visual;

import ch.njol.skript.Skript;
Expand Down Expand Up @@ -60,7 +42,6 @@ public class VisualEffects {
private static VisualEffectType[] visualEffectTypes;

static {
Variables.yggdrasil.registerSingleClass(VisualEffectType.class, "VisualEffect.NewType");
Variables.yggdrasil.registerSingleClass(Effect.class, "Bukkit_Effect");
Variables.yggdrasil.registerSingleClass(EntityEffect.class, "Bukkit_EntityEffect");
}
Expand All @@ -77,6 +58,14 @@ public static VisualEffectType get(int i) {
return visualEffectTypes[i];
}

public static @Nullable VisualEffectType get(String id) {
for (VisualEffectType type : visualEffectTypes) {
if (id.equals(type.getId()))
return type;
}
return null;
}

public static String getAllNames() {
List<Noun> names = new ArrayList<>();
for (VisualEffectType visualEffectType : visualEffectTypes) {
Expand Down