Skip to content

Commit

Permalink
Merge branch 'dev/feature' into dev/feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Jul 24, 2024
2 parents 4db8eb5 + 2f4232d commit 9a77531
Show file tree
Hide file tree
Showing 59 changed files with 486 additions and 620 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel=true

groupid=ch.njol
name=skript
version=2.9.0-pre2
version=2.9.0
jarName=Skript.jar
testEnv=java21/paper-1.21.0
testEnvJavaVersion=21
36 changes: 17 additions & 19 deletions src/main/java/ch/njol/skript/classes/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
* isn't overridden.
* <p>
* Some useful Changers can be found in {@link DefaultChangers}
*
* @author Peter Güttinger
*
* @see DefaultChangers
* @see Expression
*/
public interface Changer<T> {

public static enum ChangeMode {
enum ChangeMode {
ADD, SET, REMOVE, REMOVE_ALL, DELETE, RESET;
}

Expand All @@ -45,45 +44,44 @@ public static enum ChangeMode {
* <p>
* Unlike {@link Expression#acceptChange(ChangeMode)} this method must not print errors.
*
* @param mode
* @param mode The {@link ChangeMode} to test.
* @return An array of types that {@link #change(Object[], Object[], ChangeMode)} accepts as its <code>delta</code> parameter (which can be arrays to denote that multiple of
* that type are accepted), or null if the given mode is not supported. For {@link ChangeMode#DELETE} and {@link ChangeMode#RESET} this can return any non-null array to
* mark them as supported.
*/
@Nullable
public abstract Class<?>[] acceptChange(ChangeMode mode);
Class<?> @Nullable [] acceptChange(ChangeMode mode);

/**
* @param what The objects to change
* @param delta An array with one or more instances of one or more of the the classes returned by {@link #acceptChange(ChangeMode)} for the given change mode (null for
* {@link ChangeMode#DELETE} and {@link ChangeMode#RESET}). <b>This can be a Object[], thus casting is not allowed.</b>
* @param mode
* @param mode The {@link ChangeMode} to test.
* @throws UnsupportedOperationException (optional) if this method was called on an unsupported ChangeMode.
*/
public abstract void change(T[] what, @Nullable Object[] delta, ChangeMode mode);
void change(T[] what, Object @Nullable [] delta, ChangeMode mode);

public static abstract class ChangerUtils {
@SuppressWarnings("unchecked")
public static <T, V> void change(final Changer<T> changer, final Object[] what, final @Nullable Object[] delta, final ChangeMode mode) {
abstract class ChangerUtils {

public static <T> void change(Changer<T> changer, Object[] what, Object @Nullable [] delta, ChangeMode mode) {
//noinspection unchecked
changer.change((T[]) what, delta, mode);
}

/**
* Tests whether an expression accepts changes of a certain type. If multiple types are given it test for whether any of the types is accepted.
*
* @param e The expression to test
* @param expression The expression to test
* @param mode The ChangeMode to use in the test
* @param types The types to test for
* @return Whether <tt>e.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
* @return Whether <tt>expression.{@link Expression#change(Event, Object[], ChangeMode) change}(event, type[], mode)</tt> can be used or not.
*/
public static boolean acceptsChange(final Expression<?> e, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] cs = e.acceptChange(mode);
if (cs == null)
public static boolean acceptsChange(final Expression<?> expression, final ChangeMode mode, final Class<?>... types) {
final Class<?>[] validTypes = expression.acceptChange(mode);
if (validTypes == null)
return false;
for (final Class<?> type : types) {
for (final Class<?> c : cs) {
if (c.isArray() ? c.getComponentType().isAssignableFrom(type) : c.isAssignableFrom(type))
for (final Class<?> validType : validTypes) {
if (validType.isArray() ? validType.getComponentType().isAssignableFrom(type) : validType.isAssignableFrom(type))
return true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,8 @@ public String toVariableNameString(FireworkEffect effect) {
if (BukkitUtils.registryExists("CAT_VARIANT")) {
catTypeClassInfo = new RegistryClassInfo<>(Cat.Type.class, Registry.CAT_VARIANT, "cattype", "cat types");
} else {
catTypeClassInfo = new EnumClassInfo<>(Cat.Type.class, "cattype", "cat types");
//noinspection unchecked, rawtypes - it is an enum on other versions
catTypeClassInfo = new EnumClassInfo<>((Class) Cat.Type.class, "cattype", "cat types");
}
Classes.registerClass(catTypeClassInfo
.user("cat ?(type|race)s?")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public World[] executeSimple(Object[][] params) {
}, DefaultClasses.LOCATION, true) {
@Override
@Nullable
public Location[] execute(FunctionEvent<?> e, Object[][] params) {
public Location[] execute(FunctionEvent<?> event, Object[][] params) {
for (int i : new int[] {0, 1, 2, 4, 5}) {
if (params[i] == null || params[i].length == 0 || params[i][0] == null)
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/effects/EffBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

/**
* This effect will call {@link BroadcastMessageEvent} as of INSERT_VERSION.
* This effect will call {@link BroadcastMessageEvent} as of 2.9.0.
*/
@Override
@SuppressWarnings("deprecation")
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand Down Expand Up @@ -110,7 +109,7 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
protected TriggerItem walk(Event event) {
debug(event, false);
//noinspection rawtypes,unchecked
((ReturnHandler) handler).returnValues(value.getArray(event));
((ReturnHandler) handler).returnValues(event, value);

TriggerSection parent = getParent();
while (parent != null && parent != handler) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/ch/njol/skript/entity/CatData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package ch.njol.skript.entity;

import ch.njol.skript.registrations.Classes;
import com.google.common.collect.Iterators;
import org.bukkit.entity.Cat;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -26,12 +28,19 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.coll.CollectionUtils;

import java.util.Objects;

public class CatData extends EntityData<Cat> {

static {
if (Skript.classExists("org.bukkit.entity.Cat"))
if (Skript.classExists("org.bukkit.entity.Cat")) {
EntityData.register(CatData.class, "cat", Cat.class, "cat");
types = Iterators.toArray(Classes.getExactClassInfo(Cat.Type.class).getSupplier().get(), Cat.Type.class);
}
}

@SuppressWarnings("NotNullFieldNotInitialized")
private static Cat.Type[] types;

private Cat.@Nullable Type race = null;

Expand All @@ -42,7 +51,7 @@ protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parse
race = ((Literal<Cat.Type>) exprs[0]).getSingle();
return true;
}

@Override
protected boolean init(@Nullable Class<? extends Cat> c, @Nullable Cat cat) {
race = (cat == null) ? Cat.Type.TABBY : cat.getCatType();
Expand All @@ -51,7 +60,7 @@ protected boolean init(@Nullable Class<? extends Cat> c, @Nullable Cat cat) {

@Override
public void set(Cat entity) {
Cat.Type type = race != null ? race : CollectionUtils.getRandom(Cat.Type.values());
Cat.Type type = race != null ? race : CollectionUtils.getRandom(types);
assert type != null;
entity.setCatType(type);
}
Expand All @@ -73,7 +82,7 @@ public EntityData getSuperType() {

@Override
protected int hashCode_i() {
return race != null ? race.hashCode() : 0;
return Objects.hashCode(race);
}

@Override
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/ch/njol/skript/entity/FrogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.bukkit.entity.Frog.Variant;
import org.eclipse.jdt.annotation.Nullable;

import java.util.Objects;

public class FrogData extends EntityData<Frog> {

static {
Expand All @@ -42,13 +44,28 @@ public FrogData() {

public FrogData(@Nullable Variant variant) {
this.variant = variant;
matchedPattern = variant != null ? variant.ordinal() + 1 : 0;
matchedPattern = 0;
if (variant == Variant.TEMPERATE)
matchedPattern = 1;
if (variant == Variant.WARM)
matchedPattern = 2;
if (variant == Variant.COLD)
matchedPattern = 3;
}

@Override
protected boolean init(Literal<?>[] exprs, int matchedPattern, SkriptParser.ParseResult parseResult) {
if (matchedPattern > 0)
variant = Variant.values()[matchedPattern - 1];
switch (matchedPattern) {
case 1:
variant = Variant.TEMPERATE;
break;
case 2:
variant = Variant.WARM;
break;
case 3:
variant = Variant.COLD;
break;
}
return true;
}

Expand Down Expand Up @@ -82,7 +99,7 @@ public EntityData getSuperType() {

@Override
protected int hashCode_i() {
return variant != null ? variant.hashCode() : 0;
return Objects.hashCode(variant);
}

@Override
Expand Down
38 changes: 27 additions & 11 deletions src/main/java/ch/njol/skript/entity/VillagerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package ch.njol.skript.entity;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
Expand All @@ -37,7 +40,7 @@
* @author Peter Güttinger
*/
public class VillagerData extends EntityData<Villager> {

/**
* Professions can be for zombies also. These are the ones which are only
* for villagers.
Expand All @@ -49,25 +52,37 @@ public class VillagerData extends EntityData<Villager> {
// NORMAL(-1), FARMER(0), LIBRARIAN(1), PRIEST(2), BLACKSMITH(3), BUTCHER(4), NITWIT(5);

Variables.yggdrasil.registerSingleClass(Profession.class, "Villager.Profession");


professions = new ArrayList<>();
if (Skript.isRunningMinecraft(1, 14)) {
EntityData.register(VillagerData.class, "villager", Villager.class, 0,
"villager", "normal", "armorer", "butcher", "cartographer",
"cleric", "farmer", "fisherman", "fletcher",
"leatherworker", "librarian", "mason", "nitwit",
"shepherd", "toolsmith", "weaponsmith");
professions = Arrays.asList(Profession.values());
// TODO obtain from the registry in the future
// This is not currently done as the ordering of the professions is important
// There is no ordering guarantee from the registry
professions = Arrays.asList(Profession.NONE, Profession.ARMORER, Profession.BUTCHER, Profession.CARTOGRAPHER,
Profession.CLERIC, Profession.FARMER, Profession.FISHERMAN, Profession.FLETCHER, Profession.LEATHERWORKER,
Profession.LIBRARIAN, Profession.MASON, Profession.NITWIT, Profession.SHEPHERD, Profession.TOOLSMITH,
Profession.WEAPONSMITH);
} else { // Post 1.10: Not all professions go for villagers
EntityData.register(VillagerData.class, "villager", Villager.class, 0,
"normal", "villager", "farmer", "librarian",
"priest", "blacksmith", "butcher", "nitwit");
// Normal is for zombie villagers, but needs to be here, since someone thought changing first element in enum was good idea :(

professions = new ArrayList<>();
for (Profession prof : Profession.values()) {
// We're better off doing stringfying the constants since these don't exist in 1.14
if (!prof.toString().equals("NORMAL") && !prof.toString().equals("HUSK"))
professions.add(prof);
try {
for (Profession prof : (Profession[]) MethodHandles.lookup().findStatic(Profession.class, "values", MethodType.methodType(Profession[].class)).invoke()) {
// We're better off doing stringfying the constants since these don't exist in 1.14
// Using String#valueOf to prevent IncompatibleClassChangeError due to Enum->Interface change
String profString = String.valueOf(prof);
if (!profString.equals("NORMAL") && !profString.equals("HUSK"))
professions.add(prof);
}
} catch (Throwable e) {
throw new RuntimeException("Failed to load legacy villager profession support", e);
}
}
}
Expand Down Expand Up @@ -116,7 +131,7 @@ public Class<? extends Villager> getType() {

@Override
protected int hashCode_i() {
return profession != null ? profession.hashCode() : 0;
return Objects.hashCode(profession);
}

@Override
Expand All @@ -133,7 +148,8 @@ protected boolean deserialize(final String s) {
if (s.isEmpty())
return true;
try {
profession = Profession.valueOf(s);
//noinspection unchecked, rawtypes - prevent IncompatibleClassChangeError due to Enum->Interface change
profession = (Profession) Enum.valueOf((Class) Profession.class, s);
return true;
} catch (final IllegalArgumentException e) {
return false;
Expand All @@ -143,7 +159,7 @@ protected boolean deserialize(final String s) {
@Override
public boolean isSupertypeOf(final EntityData<?> e) {
if (e instanceof VillagerData)
return profession == null || ((VillagerData) e).profession == profession;
return profession == null || Objects.equals(((VillagerData) e).profession, profession);
return false;
}

Expand Down
Loading

0 comments on commit 9a77531

Please sign in to comment.