> For the complete documentation index, see [llms.txt](https://little-ming.gitbook.io/sbplaceholder2-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://little-ming.gitbook.io/sbplaceholder2-wiki/zuo-wei-kai-fa-zhe/zi-ding-yi-lei-xing.md).

# 自定义类型

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

{% code lineNumbers="true" %}

```java
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();
    }
}
```

{% endcode %}

### 方法详解

`public Plugin getPlugin()` &#x20;

* 获取提供插件。

`public String getName()` &#x20;

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

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

* 用于构造新元素。

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

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

```java
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);
    }
}

```

## 快速构建

```java
@EventHandler
public void onFastElementBuild(FastElementBuildEvent event) {
    if (event.getRaw_string().equals("随机数字")) {
        event.setResult(new NumberElement(Math.random()));
    }
}
```

```
> %s_随机数字%
0.58272153
```
