# Классы

Создадим класс, который будет отвечать за данные <mark style="color:blue;">**entity**</mark>

## Пример

```java
public class EntityData {
    
}
```

Также нужно создать конструктор, который позволит динамически создавать класс, который не будет постоянно храниться в памяти. В любой другом ЯП мы бы писали следующим образом - `public EntityData(){}`, но в <mark style="color:blue;">**zenScript**</mark> стоит писать `public this(){}`.

## Пример

```java
public class  EntityData {
    public this() {
        
    }
}
```

Следует ещё добавить в класс наше существо или же <mark style="color:blue;">**Entity**</mark>. Необходимо импортировать его так - `import crafttweaker.api.entity.Entity;`. Кроме этого, нужно хранить данные или же <mark style="color:blue;">**IData**</mark>. Импортируем - `import crafttweaker.api.data.IData`.

## Пример

```java
import crafttweaker.api.entity.Entity;
import crafttweaker.api.data.IData;

public class EntityData {
    public val entity as Entity;
    public val data as IData;
    
    public this(entity as Entit, data as IDatay) {
        this.entity = entity;
        this.data = data;
    }
}
```

Теперь нам важно создать метод, который будет проверять, есть ли у существа или же <mark style="color:blue;">**Entity**</mark> соответствующие данные. Поможет в этом <mark style="color:blue;">**customData**</mark>, так как в data мы записать ничего не сможем. Ключевое слово <mark style="color:blue;">**in**</mark> аналогично <mark style="color:blue;">**contains & includes**</mark>.

```typescript
public isHaveData() as bool {
    if (this.data in this.entity.customData) return true;
    else return false;
}
```

Таким образом, у нас есть метод, который позволит проверить, есть ли у игрока нужные нам данные. После этого создадим нужно класс, который сможет записать в игрока новые данные.

## Пример

```typescript
public sendData() as bool {
    if (isHaveData()) return false;
    else this.entity.updateCustomData(this.data);
}
```

Теперь нас есть всё необходимое. Остаётся только применить данный класс. `crafttweaker.api.event.entity.player.ItemPickupEvent`.

## Пример

```java
import crafttweaker.api.events.CTEventManager;
import crafttweaker.api.event.entity.player.ItemPickupEvent;
CTEventManager.register<crafttweaker.api.event.entity.player.ItemPickupEvent>((event) => {
    var player = event.player;
    if (player.level.isClientSide) return;
    var myData as EntityData = new EntityData(player, {"firstPickUp" : true});
    myData.sendData();
});
```

При подборе любого предмета в игрока будут внесены новые данные в <mark style="color:blue;">**customData**</mark>. Вы можете задаться вопросом, как же мы передали player в наш класс, если в его параметрах <mark style="color:blue;">**Entity**</mark>? Всё потому, что игрок наследуется от <mark style="color:blue;">**EntityLiving**</mark>, а он, в свою очередь, от <mark style="color:blue;">**Entity**</mark>.

## Полный код

```typescript
import crafttweaker.api.entity.Entity;
import crafttweaker.api.data.IData;

public class EntityData {
    public val entity as Entity;
    public val data as IData;
    
    public this (entity as Entit, data as IDatay) {
        this.entity = entity;
        this.data = data;
    }

    public isHaveData() as bool {
        if (this.data in this.entity.customData) return true;
        else return false;
    }
    
    public sendData() as bool {
        if (isHaveData()) return false;
        else this.entity.updateCustomData(this.data);
    }
}
```


---

# 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://wiki.aegis-mine.ru/modifications/sozdanie-kontenta/crafttweaker/minecraft-1.19.2/klassy.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.
