🤩
SBPlaceholder2 Wiki
  • SBPlaceholder 2
  • 基础信息
    • 🤔这是什么
    • 😇从何而来
    • 😘插件安装
  • 初窥门径
    • 🐥第一个表达式
    • 👨‍🍳元素与运算
    • 👨‍⚕️外部占位符
  • 融会贯通
    • 🔥简单元素类型
    • 🌪️函数调用
    • ☃️方法调用
  • 炉火纯青
    • 👨‍🏫复杂元素类型
    • 🧙‍♂️发生了什么
  • 一些例子
    • 👶宝宝难度
    • 👩‍🦳困难难度
    • 🦊大师难度
  • 百科全书
    • 🚢内置类型
      • 整数 Int
      • 数值 Number
      • 文本 String
      • 布尔值 Bool
      • 子表达式 Expression
      • 玩家 Player
      • 函数 Function
      • 虚空 Void
      • *列表 List
      • *字典 Dict
      • *类型 Type
    • 🚓内置函数
    • 🗿更新日志
  • 作为开发者
    • 🎷自定义函数
    • 🎸自定义类型
    • 🎤拓展方法
由 GitBook 提供支持
在本页
  • 方法详解
  • 下面是内置元素整数的实现
  • 快速构建
  1. 作为开发者

自定义类型

创建自定义类型需要定义两个类,分别继承 SBType 与 SBElement,我叫他类型与元素,类型用于创建元素,元素直接参与表达式解析,下面是内置类型整数的实现以供参考。

public class IntType extends SBType<IntElement> {
    public static IntType inst = new IntType();
    private IntType() {}
    
    @Override
    public Plugin getPlugin() {
        return SBPlaceholder2.plugin;
    }
    
    @Override
    public String getName() {
        return "Int";
    }
    
    @Override
    public IntElement newInst(Parser parser, EntrustInst... insts) {
        return insts[0].execute(parser).asInt();
    }
}

方法详解

public Plugin getPlugin()

  • 获取提供插件。

public String getName()

  • 获取类型名,返回值应与对应的元素的 getName() 方法返回值相同。

public IntElement newInst(Parser parser, EntrustInst... insts)

  • 用于构造新元素。

最后执行 IntType.inst.register("Int") 注册类型。

下面是内置元素整数的实现

public class IntElement extends SBElement<IntType> {
    @NotNull public final Integer value;
    
    public IntElement(@NotNull Integer value) {
        this.value = value;
    }
    
    @Override public Plugin getPlugin() { return SBPlaceholder2.plugin; }
    @Override public String getName() { return "Int"; }
    @Override public String toString() { return value.toString(); }
    
    @Override
    public BoolElement asBool() {
        return BoolElement.fromBool(value > 0);
    }
    
    @Override
    public IntElement asInt() {
        return this;
    }
    
    @Override
    public NumberElement asNumber() {
        return new NumberElement(value.doubleValue());
    }
    
    @Override
    public IntElement symbol_add(SBElement<?> other) {
        return new IntElement(value + other.asInt().value);
    }
    
    @Override
    public IntElement symbol_sub(SBElement<?> other) {
        return new IntElement(value - other.asInt().value);
    }
    
    @Override
    public IntElement symbol_mul(SBElement<?> other) {
        return new IntElement(value * other.asInt().value);
    }
    
    @Override
    public NumberElement symbol_div(SBElement<?> other) {
        return new NumberElement(((double)value) / other.asInt().value);
    }
    
    @Override
    public IntElement symbol_double_div(SBElement<?> other) {
        return new IntElement(value / other.asInt().value);
    }
    
    @Override
    public NumberElement symbol_double_mul(SBElement<?> other) {
        return new NumberElement(Math.pow((double)value, other.asNumber().value));
    }
    
    @Override
    public Integer symbol_compare(SBElement<?> other) {
        return value.compareTo(other.asInt().value);
    }
    
    @ElementMethod(name = "abs", returnType = "Int")
    public SBElement<?> method_abs(Parser parser, EntrustInst... args) {
        return new IntElement(Math.abs(value));
    }
    
    @ElementMethod(name = "mod", args = {"Int"}, returnType = "Int")
    public SBElement<?> method_mod(Parser parser, EntrustInst... args) {
        IntElement arg1 = args[0].execute(parser).asInt();
        return new IntElement(value % arg1.value);
    }
    
    @Override
    public int hashCode() {
        return value.hashCode();
    }
    
    @Override
    public boolean equals(Object obj) {
        return obj instanceof IntElement intInst && intInst.value.equals(value);
    }
}

快速构建

@EventHandler
public void onFastElementBuild(FastElementBuildEvent event) {
    if (event.getRaw_string().equals("随机数字")) {
        event.setResult(new NumberElement(Math.random()));
    }
}
> %s_随机数字%
0.58272153
上一页自定义函数下一页拓展方法

最后更新于1年前

🎸