Skip to content
Neovitalism edited this page Sep 3, 2024 · 3 revisions

Introduction to NeoClear's API

NeoClear has API to add clear types for entities that are not supported by the default clear types, or for finer tuning. Below is an example to explain the API.

The Example

In this example, we'll make a clear type that clears unoccupied boat entities, with a whitelist for specific boats. First, we want to make a class that extends ClearType<T>, with BoatEntity as <T>, and a constructor calling super.

public class BoatClearType extends ClearType<BoatEntity> {
    public BoatClearType(Configuration config) {
        super(config);
    }
}

Next, let's set up the whitelist and it's functionality. Override the isWhitelisted(T) method. Here, we'll check for the type of boat this boat is. To do so, let's get the entity as an item, and have the item's identifier be the whitelist, like so:

    @Override
    public boolean isWhitelisted(BoatEntity boatEntity) {
        Identifier itemID = Registries.ITEM.getId(boatEntity.asItem());
        String stringID = itemID.toString();
        return this.whitelist.contains(stringID);
    }

Now let's handle the clear itself. Override the clear(List<ServerWorld>) method. First, we need to create a long to return after we're done. After that, we loop over the worlds and collect the entities in that world of our type, with a predicate making sure the boat is not occupied by an entity or a chest. We also check if the entity is null to deal with some odd incompatibilities in certain mods. For the rest of this section, we'll loop over the entities, discard them, and increment the clearCount to return. All put together should look like this:

    @Override
    protected long clear(List<ServerWorld> worlds) {
        long clearCount = 0;
        for (ServerWorld world : worlds) {
            List<? extends BoatEntity> entities = world.getEntitiesByType(
                    TypeFilter.instanceOf(BoatEntity.class), entity -> {
                if (entity == null) return false;
                if (entity instanceof ChestBoatEntity) return false;
                if (entity.hasPassengers()) return false;
                return !isWhitelisted(entity);
            });
            for (BoatEntity entity : entities) {
                entity.discard();
                clearCount++;
            }
        }
        return clearCount;
    }

That's it for the clear type class! To register the clear type, you'll simply register the type with the ClearTypeRegistry, like so:

ClearTypeRegistry.register("BOAT", BoatClearType.class);

Now our BOAT clear type is done! To use this type, you'd set the type in the config to BOAT. The whitelist uses the boat's item identifier, just like the Item clear type.

All Put Together

If you're actually interested in this clear type, for some reason, here's the full class:

import me.neovitalism.neoapi.modloading.config.Configuration;
import me.neovitalism.neoclear.api.cleartypes.ClearType;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.entity.vehicle.ChestBoatEntity;
import net.minecraft.registry.Registries;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypeFilter;

import java.util.List;

public class BoatClearType extends ClearType<BoatEntity> {
    public BoatClearType(Configuration config) {
        super(config);
    }

    @Override
    public boolean isWhitelisted(BoatEntity boatEntity) {
        Identifier itemID = Registries.ITEM.getId(boatEntity.asItem());
        String stringID = itemID.toString();
        return this.whitelist.contains(stringID);
    }

    @Override
    protected long clear(List<ServerWorld> worlds) {
        long clearCount = 0;
        for (ServerWorld world : worlds) {
            List<? extends BoatEntity> entities = world.getEntitiesByType(
                    TypeFilter.instanceOf(BoatEntity.class), entity -> {
                if(entity == null) return false;
                if(entity instanceof ChestBoatEntity) return false;
                if(entity.hasPassengers()) return false;
                return !isWhitelisted(entity);
            });
            for(BoatEntity entity : entities) {
                entity.discard();
                clearCount++;
            }
        }
        return clearCount;
    }
}

Clone this wiki locally