177 lines
7.1 KiB
Java
177 lines
7.1 KiB
Java
package com.bgsoftware.wildloaders.nms.v1_17_R1;
|
|
|
|
import com.bgsoftware.common.reflection.ReflectMethod;
|
|
import com.bgsoftware.wildloaders.WildLoadersPlugin;
|
|
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
|
|
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
|
|
import com.bgsoftware.wildloaders.nms.v1_17_R1.loader.TileEntityChunkLoader;
|
|
import net.minecraft.core.BlockPosition;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.nbt.NBTTagList;
|
|
import net.minecraft.nbt.NBTTagLong;
|
|
import net.minecraft.nbt.NBTTagString;
|
|
import net.minecraft.server.level.WorldServer;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.ChunkCoordIntPair;
|
|
import net.minecraft.world.level.World;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
|
import net.minecraft.world.level.block.entity.TickingBlockEntity;
|
|
import net.minecraft.world.level.block.entity.TileEntity;
|
|
import net.minecraft.world.level.block.entity.TileEntityMobSpawner;
|
|
import net.minecraft.world.level.block.state.IBlockData;
|
|
import net.minecraft.world.level.chunk.Chunk;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.craftbukkit.v1_17_R1.CraftChunk;
|
|
import org.bukkit.craftbukkit.v1_17_R1.CraftServer;
|
|
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
|
|
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
|
|
import org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers;
|
|
|
|
import java.util.UUID;
|
|
|
|
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
|
|
|
|
@Override
|
|
public String getTag(org.bukkit.inventory.ItemStack itemStack, String key, String def) {
|
|
ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
|
|
NBTTagCompound tagCompound = nmsItem.getOrCreateTag();
|
|
|
|
if (!tagCompound.hasKeyOfType(key, 8))
|
|
return def;
|
|
|
|
return tagCompound.getString(key);
|
|
}
|
|
|
|
@Override
|
|
public org.bukkit.inventory.ItemStack setTag(org.bukkit.inventory.ItemStack itemStack, String key, String value) {
|
|
ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
|
|
NBTTagCompound tagCompound = nmsItem.getOrCreateTag();
|
|
|
|
tagCompound.set(key, NBTTagString.a(value));
|
|
|
|
nmsItem.setTag(tagCompound);
|
|
|
|
return CraftItemStack.asBukkitCopy(nmsItem);
|
|
}
|
|
|
|
@Override
|
|
public long getTag(org.bukkit.inventory.ItemStack itemStack, String key, long def) {
|
|
ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
|
|
NBTTagCompound tagCompound = nmsItem.getOrCreateTag();
|
|
|
|
if (!tagCompound.hasKeyOfType(key, 4))
|
|
return def;
|
|
|
|
return tagCompound.getLong(key);
|
|
}
|
|
|
|
@Override
|
|
public org.bukkit.inventory.ItemStack setTag(org.bukkit.inventory.ItemStack itemStack, String key, long value) {
|
|
ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
|
|
NBTTagCompound tagCompound = nmsItem.getOrCreateTag();
|
|
|
|
tagCompound.set(key, NBTTagLong.a(value));
|
|
|
|
nmsItem.setTag(tagCompound);
|
|
|
|
return CraftItemStack.asBukkitCopy(nmsItem);
|
|
}
|
|
|
|
@Override
|
|
public org.bukkit.inventory.ItemStack getPlayerSkull(org.bukkit.inventory.ItemStack itemStack, String texture) {
|
|
ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);
|
|
|
|
NBTTagCompound nbtTagCompound = nmsItem.getOrCreateTag();
|
|
|
|
NBTTagCompound skullOwner = nbtTagCompound.hasKey("SkullOwner") ? nbtTagCompound.getCompound("SkullOwner") : new NBTTagCompound();
|
|
|
|
NBTTagCompound properties = new NBTTagCompound();
|
|
|
|
NBTTagList textures = new NBTTagList();
|
|
NBTTagCompound signature = new NBTTagCompound();
|
|
signature.setString("Value", texture);
|
|
textures.add(signature);
|
|
|
|
properties.set("textures", textures);
|
|
|
|
skullOwner.set("Properties", properties);
|
|
skullOwner.setString("Id", UUID.randomUUID().toString());
|
|
|
|
nbtTagCompound.set("SkullOwner", skullOwner);
|
|
|
|
nmsItem.setTag(nbtTagCompound);
|
|
|
|
return CraftItemStack.asBukkitCopy(nmsItem);
|
|
}
|
|
|
|
@Override
|
|
public com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC createNPC(Location location, UUID uuid) {
|
|
return new ChunkLoaderNPC(((CraftServer) Bukkit.getServer()).getServer(), location, uuid);
|
|
}
|
|
|
|
@Override
|
|
public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) {
|
|
Location loaderLoc = chunkLoader.getLocation();
|
|
assert loaderLoc.getWorld() != null;
|
|
WorldServer world = ((CraftWorld) loaderLoc.getWorld()).getHandle();
|
|
BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ());
|
|
|
|
TileEntityChunkLoader tileEntityChunkLoader = new TileEntityChunkLoader(chunkLoader, world, blockPosition);
|
|
world.a(tileEntityChunkLoader.ticker);
|
|
|
|
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
|
|
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
|
|
chunk.l.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner)
|
|
.forEach(tileEntity -> ((TileEntityMobSpawner) tileEntity).getSpawner().n = -1);
|
|
|
|
world.setForceLoaded(chunk.getPos().b, chunk.getPos().c, true);
|
|
}
|
|
|
|
return tileEntityChunkLoader;
|
|
}
|
|
|
|
@Override
|
|
public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) {
|
|
Location loaderLoc = chunkLoader.getLocation();
|
|
assert loaderLoc.getWorld() != null;
|
|
WorldServer world = ((CraftWorld) loaderLoc.getWorld()).getHandle();
|
|
BlockPosition blockPosition = new BlockPosition(loaderLoc.getX(), loaderLoc.getY(), loaderLoc.getZ());
|
|
|
|
long tileEntityLong = ChunkCoordIntPair.pair(blockPosition.getX() >> 4, blockPosition.getZ() >> 4);
|
|
TileEntityChunkLoader tileEntityChunkLoader = TileEntityChunkLoader.tileEntityChunkLoaderMap.remove(tileEntityLong);
|
|
if (tileEntityChunkLoader != null) {
|
|
tileEntityChunkLoader.holograms.forEach(EntityHolograms::removeHologram);
|
|
tileEntityChunkLoader.removed = true;
|
|
}
|
|
|
|
if (spawnParticle)
|
|
world.a(null, 2001, blockPosition, Block.getCombinedId(world.getType(blockPosition)));
|
|
|
|
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
|
|
Chunk chunk = ((CraftChunk) bukkitChunk).getHandle();
|
|
chunk.l.values().stream().filter(tileEntity -> tileEntity instanceof TileEntityMobSpawner)
|
|
.forEach(tileEntity -> ((TileEntityMobSpawner) tileEntity).getSpawner().n = 16);
|
|
|
|
world.setForceLoaded(chunk.getPos().b, chunk.getPos().c, false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void updateSpawner(Location location, boolean reset) {
|
|
assert location.getWorld() != null;
|
|
World world = ((CraftWorld) location.getWorld()).getHandle();
|
|
|
|
BlockPosition blockPosition = new BlockPosition(location.getX(), location.getY(), location.getZ());
|
|
IBlockData blockData = world.getType(blockPosition);
|
|
TileEntityMobSpawner mobSpawner = (TileEntityMobSpawner) world.getTileEntity(blockPosition);
|
|
|
|
if (mobSpawner == null)
|
|
return;
|
|
|
|
mobSpawner.getSpawner().n = reset ? 16 : -1;
|
|
}
|
|
|
|
}
|