mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-22 15:22:56 +01:00
Add more TagHandler implementations
This commit is contained in:
parent
6526a2658a
commit
76bec54254
@ -19,9 +19,10 @@ public interface DataContainer {
|
||||
* meaning that this will be null if no data has been defined.
|
||||
*
|
||||
* @return the {@link Data} of this container, can be null
|
||||
* @deprecated use the tag API https://wiki.minestom.com/feature/tags
|
||||
*/
|
||||
@Nullable
|
||||
Data getData();
|
||||
@Deprecated
|
||||
@Nullable Data getData();
|
||||
|
||||
/**
|
||||
* Sets the {@link Data} of this container.
|
||||
@ -30,7 +31,8 @@ public interface DataContainer {
|
||||
* on your use-case.
|
||||
*
|
||||
* @param data the new {@link Data} of this container, null to remove it
|
||||
* @deprecated use the tag API https://wiki.minestom.com/feature/tags
|
||||
*/
|
||||
@Deprecated
|
||||
void setData(@Nullable Data data);
|
||||
|
||||
}
|
@ -8,8 +8,11 @@ import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.LivingEntity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.sound.SoundEvent;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
/**
|
||||
* Represents a type of damage, required when calling {@link LivingEntity#damage(DamageType, float)}
|
||||
@ -19,7 +22,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
* Be aware that this class implements {@link DataContainer}
|
||||
* so you can add your own data to an already existing damage type without any wrapper.
|
||||
*/
|
||||
public class DamageType implements DataContainer {
|
||||
public class DamageType implements TagHandler, DataContainer {
|
||||
|
||||
public static final DamageType VOID = new DamageType("attack.outOfWorld");
|
||||
public static final DamageType GRAVITY = new DamageType("attack.fall");
|
||||
@ -30,6 +33,8 @@ public class DamageType implements DataContainer {
|
||||
}
|
||||
};
|
||||
private final String identifier;
|
||||
private final Object nbtLock = new Object();
|
||||
private final NBTCompound nbt = new NBTCompound();
|
||||
private Data data;
|
||||
|
||||
/**
|
||||
@ -159,4 +164,18 @@ public class DamageType implements DataContainer {
|
||||
public void setData(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
synchronized (nbtLock) {
|
||||
return tag.read(nbt);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
synchronized (nbtLock) {
|
||||
tag.write(nbt, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ import net.minestom.server.instance.block.CustomBlock;
|
||||
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
|
||||
import net.minestom.server.network.packet.server.play.UpdateLightPacket;
|
||||
import net.minestom.server.network.player.PlayerConnection;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagHandler;
|
||||
import net.minestom.server.utils.PacketUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.binary.BinaryReader;
|
||||
@ -27,6 +29,7 @@ import net.minestom.server.world.biomes.Biome;
|
||||
import net.minestom.server.world.biomes.BiomeManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -47,7 +50,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* You generally want to avoid storing references of this object as this could lead to a huge memory leak,
|
||||
* you should store the chunk coordinates instead.
|
||||
*/
|
||||
public abstract class Chunk implements Viewable, Tickable, DataContainer {
|
||||
public abstract class Chunk implements Viewable, Tickable, TagHandler, DataContainer {
|
||||
|
||||
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
||||
protected static final BiomeManager BIOME_MANAGER = MinecraftServer.getBiomeManager();
|
||||
@ -75,6 +78,7 @@ public abstract class Chunk implements Viewable, Tickable, DataContainer {
|
||||
protected PFColumnarSpace columnarSpace;
|
||||
|
||||
// Data
|
||||
private final NBTCompound nbt = new NBTCompound();
|
||||
protected Data data;
|
||||
|
||||
public Chunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) {
|
||||
@ -480,6 +484,16 @@ public abstract class Chunk implements Viewable, Tickable, DataContainer {
|
||||
return unmodifiableViewers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
return tag.read(nbt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
tag.write(nbt, value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Data getData() {
|
||||
|
@ -28,6 +28,8 @@ import net.minestom.server.instance.block.CustomBlock;
|
||||
import net.minestom.server.network.packet.server.play.BlockActionPacket;
|
||||
import net.minestom.server.network.packet.server.play.TimeUpdatePacket;
|
||||
import net.minestom.server.storage.StorageLocation;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagHandler;
|
||||
import net.minestom.server.thread.ThreadProvider;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.PacketUtils;
|
||||
@ -43,6 +45,7 @@ import net.minestom.server.world.DimensionType;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -60,7 +63,7 @@ import java.util.function.Consumer;
|
||||
* you need to be sure to signal the {@link UpdateManager} of the changes using
|
||||
* {@link UpdateManager#signalChunkLoad(Chunk)} and {@link UpdateManager#signalChunkUnload(Chunk)}.
|
||||
*/
|
||||
public abstract class Instance implements BlockModifier, Tickable, EventHandler<InstanceEvent>, DataContainer, PacketGroupingAudience {
|
||||
public abstract class Instance implements BlockModifier, Tickable, TagHandler, PacketGroupingAudience, EventHandler<InstanceEvent>, DataContainer {
|
||||
|
||||
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
||||
protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();
|
||||
@ -101,6 +104,8 @@ public abstract class Instance implements BlockModifier, Tickable, EventHandler<
|
||||
protected final Queue<Consumer<Instance>> nextTick = new ConcurrentLinkedQueue<>();
|
||||
|
||||
// instance custom data
|
||||
private final Object nbtLock = new Object();
|
||||
private final NBTCompound nbt = new NBTCompound();
|
||||
private Data data;
|
||||
|
||||
// the explosion supplier
|
||||
@ -1061,6 +1066,20 @@ public abstract class Instance implements BlockModifier, Tickable, EventHandler<
|
||||
this.worldBorder.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
synchronized (nbtLock) {
|
||||
return tag.read(nbt);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
synchronized (nbtLock) {
|
||||
tag.write(nbt, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an explosion at the given position with the given strength.
|
||||
* The algorithm used to compute damages is provided by {@link #getExplosionSupplier()}.
|
||||
|
@ -5,10 +5,13 @@ import net.minestom.server.data.DataContainer;
|
||||
import net.minestom.server.inventory.click.InventoryClickProcessor;
|
||||
import net.minestom.server.inventory.condition.InventoryCondition;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagHandler;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -19,7 +22,7 @@ import java.util.function.UnaryOperator;
|
||||
/**
|
||||
* Represents an inventory where items can be modified/retrieved.
|
||||
*/
|
||||
public abstract class AbstractInventory implements InventoryClickHandler, DataContainer {
|
||||
public abstract class AbstractInventory implements InventoryClickHandler, TagHandler, DataContainer {
|
||||
|
||||
private final int size;
|
||||
protected final ItemStack[] itemStacks;
|
||||
@ -29,6 +32,8 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
|
||||
// the click processor which process all the clicks in the inventory
|
||||
protected final InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
|
||||
|
||||
private final Object nbtLock = new Object();
|
||||
private final NBTCompound nbt = new NBTCompound();
|
||||
private Data data;
|
||||
|
||||
protected AbstractInventory(int size) {
|
||||
@ -210,6 +215,20 @@ public abstract class AbstractInventory implements InventoryClickHandler, DataCo
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
synchronized (nbtLock) {
|
||||
return tag.read(nbt);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
synchronized (nbtLock) {
|
||||
tag.write(nbt, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Data getData() {
|
||||
return data;
|
||||
|
Loading…
Reference in New Issue
Block a user