SkriptLang / Skript

Skript is a Spigot plugin which allows server admins to customize their server easily, but without the hassle of programming a plugin or asking/paying someone to program a plugin for them.
https://docs.skriptlang.org
GNU General Public License v3.0
1.08k stars 371 forks source link

TypeInfos of custom class is not getting registered in custom events. #5880

Open DereWah opened 1 year ago

DereWah commented 1 year ago

Skript/Server Version

Skript 2.7-beta
Minecraft 1.19.4

Bug Description

I've been trying to update my addon to Skript 2.7, but I am getting an error and i have no idea where that is from.

Note that the addon works perfectly with Skript 2.6.4, it's just the version jump. https://github.com/DereWah/Skript-AnvilGUI here is the source code, the error is image

From the code

on anvil gui click: if title of event-anvil gui is "&0Inserisci codice": In the console there are no errors when loading the plugin. It just seems like adding event values has changed since the 2 versions, and I don't know what is the different thing to do Initially I thought the issue was naming the class "anvil", as it would create problems with a possible new existing class, but this error persists even if I call the classinfo virtualanvil or anything different.

Specifically, here's the event class where I register the event values


package org.derewah.skriptanvilgui.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;
import org.bukkit.entity.Player;
import org.derewah.skriptanvilgui.anvilgui.Anvil;
import org.derewah.skriptanvilgui.events.Anvil.BridgeAnvilClick;

public class EvtOnAnvilGUIClick {

    static{
        Skript.registerEvent("On AnvilGUI Click", SimpleEvent.class, BridgeAnvilClick.class, "anvil gui click");
        EventValues.registerEventValue(BridgeAnvilClick.class, String.class, new Getter<String, BridgeAnvilClick>() {
            @Override
            public String get(BridgeAnvilClick bridgeAnvilClick) {
                return bridgeAnvilClick.getAnvilState().getText();
            }
        }, EventValues.TIME_NOW);
        EventValues.registerEventValue(BridgeAnvilClick.class, Player.class, new Getter<Player, BridgeAnvilClick>() {
            @Override
            public Player get(BridgeAnvilClick bridgeAnvilClick) {
                return bridgeAnvilClick.getAnvilState().getPlayer();
            }
        }, EventValues.TIME_NOW);
        EventValues.registerEventValue(BridgeAnvilClick.class, Integer.class, new Getter<Integer, BridgeAnvilClick>() {
            @Override
            public Integer get(BridgeAnvilClick bridgeAnvilClick) {
                return bridgeAnvilClick.getAnvilSlot();
            }
        }, EventValues.TIME_NOW);
        EventValues.registerEventValue(BridgeAnvilClick.class, Anvil.class, new Getter<Anvil, BridgeAnvilClick>() {
            @Override
            public Anvil get(BridgeAnvilClick bridgeAnvilClick) {
                return bridgeAnvilClick.getAnvil();
            }
        }, EventValues.TIME_NOW);
    }

}

Expected Behavior

The custom event should have the custom event value registered.

Steps to Reproduce

Add a custom event value with a custom class to an event

Errors or Screenshots

No response

Other

No response

Agreement

TheLimeGlass commented 1 year ago

Can you send your .lang file?

You have to have a default.lang or LANGUAGE.lang file registered to your addon using SkriptAddon.html#setLanguageFileDirectory(java.lang.String) when registering types

DereWah commented 1 year ago

I don't have any lang file created in my project. Should I create one such as

version: 1.3

types:
    virtualanvil: anvil gui

and then set the file directory in the addon registration code?

(For the lang file above I followed the lang at https://github.com/ShaneBeee/SkBee/blob/master/src/main/resources/lang/english.lang)

DereWah commented 1 year ago

Even with the lang file, the error still exists,but slightly different:

image

DereWah commented 1 year ago

I think I solved the issue: I changed the userInputPattern of the anvil type from "anvil gui" to "virtual anvil" and it seems to now work. Is it because the pattern "anvil gui" is already taken by Skript? I don't see it used but it seems to be the issue.

DereWah commented 1 year ago

I've been testing this for the last hour, and I have no clue what's going on. Here's all I tried. Please note that I would like to not change the syntax of the addon, as there are already a lot of skripts that use it published.

types:
    anvil: anvil gui¦s

and now only the expression "anvil guis" is recognized. Only the plural, NOT the singular. In the TypeInfo class, I have this:

package org.derewah.skriptanvilgui;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import net.wesjd.anvilgui.AnvilGUI;
import org.derewah.skriptanvilgui.anvilgui.Anvil;

public class Types {

    static {
        Classes.registerClass(new ClassInfo<>(Anvil.class, "anvil")
                .user("anvil gui[s]")
                .defaultExpression(new EventValueExpression<>(Anvil.class))
                .name("anvil gui")
                .description("Represents an anvil gui.")
                .parser(new Parser<Anvil>(){

                    @Override
                    public boolean canParse(ParseContext parseContext){
                        return false;
                    }

                    @Override
                    public String toString(Anvil anvil, int debug){
                        return anvil.toString();
                    }

                    @Override
                    public String toVariableNameString(Anvil anvil){
                        return anvil.toString();
                    }
                })
        );
    }
}

As you can see the allowed input patterns allow for the plural, but because of the lang file only that one gets recognized.

Here is a SimplePropertyExpression that makes use of the anvil TypeInfo.

package org.derewah.skriptanvilgui.expressions;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.derewah.skriptanvilgui.anvilgui.Anvil;

public class ExprAnvilGUITitle extends SimplePropertyExpression<Anvil, String> {

    static {
        register(ExprAnvilGUITitle.class, String.class, "title", "anvil");
    }

    @Override
    public String convert(Anvil anvil) {
        return anvil.getTitle();
    }

    @Override
    public Class<?>[] acceptChange(final ChangeMode mode){
        if (mode == ChangeMode.SET) {return CollectionUtils.array(String.class);}
        return null;
    }

    @Override
    public void change(Event event, Object[] delta, ChangeMode mode) {
        Anvil anvil = getExpr().getSingle(event);
        if(anvil != null && delta != null){
            anvil.setTitle((String) delta[0]);
        }
    }

    @Override
    public Class<? extends String> getReturnType() {
        return String.class;
    }

    @Override
    protected String getPropertyName() {
        return "title";
    }
}

Whenever I use it in an event such as on anvil gui click, it throws this error on skript reload:

image

But if I change it to plural, it says

image

So we have two errors here:

DereWah commented 1 year ago

After further investigation, I'm 99% there's already a ClassInfo that occupies the name "anvil gui".

I changed the userInputPattern to a proper regEx "(?:virtual(?: )?)?anvil(?:(?: )?gui)?"

And also I set the lang file to types: virtualanvil: [virtual[ ]]anvil[[ ]gui] (even tho I think the square brackets are useless now, as the lang file is not for parsing but for errors and formatting (?))

Now, whenever I use "virtual anvil" or "virtualanvil" or "anvil" it works perfectly.

However, when I use "anvilgui" or "anvil gui", it says

image

I tried running on a server with only Skript and the addon and the error is still there. So it's Skript.

DereWah commented 1 year ago

Any updates on this? Anyone has any idea?