# 自定义类型

创建自定义类型需要定义两个类，分别继承 `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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://little-ming.gitbook.io/sbplaceholder2-wiki/zuo-wei-kai-fa-zhe/zi-ding-yi-lei-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
