Support to 1.20.5 (#92)

This commit is contained in:
Ome_R 2024-04-29 19:50:42 +03:00 committed by GitHub
parent a07499aa66
commit 3d021891a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 893 additions and 862 deletions

View File

@ -3,6 +3,7 @@ package com.bgsoftware.wildloaders.nms.v1_12_R1;
import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_12_R1.loader.TileEntityChunkLoader;
import net.minecraft.server.v1_12_R1.Block;
import net.minecraft.server.v1_12_R1.BlockPosition;
@ -23,7 +24,7 @@ import org.bukkit.craftbukkit.v1_12_R1.util.LongHash;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
private static final ReflectMethod<Void> TILE_ENTITY_LOAD = new ReflectMethod<>(TileEntity.class, "load", NBTTagCompound.class);

View File

@ -1,8 +1,8 @@
package com.bgsoftware.wildloaders.nms.v1_16_R3;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_16_R3.loader.TileEntityChunkLoader;
import net.minecraft.server.v1_16_R3.Block;
import net.minecraft.server.v1_16_R3.BlockPosition;
@ -24,7 +24,7 @@ import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack itemStack, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_17;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_17.loader.ChunkLoaderBlockEntity;
import com.bgsoftware.wildloaders.nms.v1_17.npc.ChunkLoaderNPCWrapper;
import net.minecraft.core.BlockPos;
@ -24,7 +25,7 @@ import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_18;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_18.loader.ChunkLoaderBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
@ -23,7 +24,7 @@ import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_19;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_19.loader.ChunkLoaderBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
@ -22,7 +23,7 @@ import org.bukkit.craftbukkit.v1_19_R3.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_20_1;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_1.loader.ChunkLoaderBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
@ -22,7 +23,7 @@ import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_20_2;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_2.loader.ChunkLoaderBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
@ -22,7 +23,7 @@ import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

View File

@ -1,5 +1,5 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.3.8"
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_20_3;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_3.loader.ChunkLoaderBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
@ -22,7 +23,7 @@ import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {

36
NMS/v1_20_4/build.gradle Normal file
View File

@ -0,0 +1,36 @@
plugins {
id("io.papermc.paperweight.userdev") version "1.6.0"
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
group 'NMS:v1_20_4'
dependencies {
paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.20.5-R0.1-SNAPSHOT")
compileOnly project(":API")
compileOnly rootProject
}
shadowJar {
archiveFileName = "${project.name}-exclude.jar"
}
assemble {
dependsOn(reobfJar)
}
tasks {
reobfJar {
File outputFile = new File(rootProject.archiveFolder, "reobf/${project.name}.jar")
outputJar.set(layout.buildDirectory.file(outputFile.getPath()))
}
}
if (project.hasProperty('nms.compile_v1_20') && !Boolean.valueOf(project.findProperty("nms.compile_v1_20").toString())) {
project.tasks.all { task -> task.enabled = false }
}

View File

@ -0,0 +1,252 @@
package com.bgsoftware.wildloaders.nms.v1_20_4;
import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC;
import com.bgsoftware.wildloaders.handlers.NPCHandler;
import com.bgsoftware.wildloaders.npc.DummyChannel;
import com.mojang.authlib.GameProfile;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.core.BlockPos;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.game.ServerboundChatPacket;
import net.minecraft.network.protocol.game.ServerboundContainerClickPacket;
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.network.protocol.game.ServerboundSetCarriedItemPacket;
import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket;
import net.minecraft.network.protocol.game.ServerboundUseItemPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.ServerAdvancementManager;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerPlayerGameMode;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.storage.LevelResource;
import net.minecraft.world.phys.AABB;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.nio.file.Path;
import java.util.UUID;
public final class ChunkLoaderNPCImpl extends ServerPlayer implements ChunkLoaderNPC {
private static final ReflectMethod<Void> SET_GAMEMODE = new ReflectMethod<>(ServerPlayerGameMode.class,
1, GameType.class, GameType.class);
private final ServerLevel serverLevel;
private final AABB boundingBox;
private final PlayerAdvancements advancements;
private boolean dieCall = false;
public ChunkLoaderNPCImpl(MinecraftServer minecraftServer, Location location, UUID uuid) {
super(minecraftServer, ((CraftWorld) location.getWorld()).getHandle(),
new GameProfile(uuid, NPCHandler.getName(location.getWorld().getName())),
ClientInformation.createDefault());
this.serverLevel = serverLevel();
this.boundingBox = new AABB(new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
this.connection = new DummyServerGamePacketListenerImpl(minecraftServer, this);
this.advancements = new DummyPlayerAdvancements(minecraftServer, this);
SET_GAMEMODE.invoke(this.gameMode, GameType.CREATIVE, null);
try {
setLoadViewDistance(2);
setTickViewDistance(2);
setSendViewDistance(2);
} catch (Throwable ignored) {
// Doesn't exist on Spigot
}
fauxSleeping = true;
spawnIn(this.serverLevel);
moveTo(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
this.serverLevel.addNewPlayer(this);
super.setBoundingBox(this.boundingBox);
}
@Override
public UUID getUniqueId() {
return super.getUUID();
}
@Override
public void die() {
discard();
}
@Override
public AABB getBoundingBoxForCulling() {
return this.boundingBox;
}
@Override
public void remove(RemovalReason removalReason) {
if (!dieCall) {
dieCall = true;
this.serverLevel.removePlayerImmediately(this, RemovalReason.UNLOADED_WITH_PLAYER);
dieCall = false;
} else {
super.remove(removalReason);
}
}
@Override
public Location getLocation() {
return getBukkitEntity().getLocation();
}
@Override
public Player getPlayer() {
return getBukkitEntity();
}
@Override
public PlayerAdvancements getAdvancements() {
return this.advancements;
}
public static class DummyConnection extends Connection {
DummyConnection() {
super(PacketFlow.SERVERBOUND);
this.channel = new DummyChannel();
this.address = null;
}
@Override
public void setListenerForServerboundHandshake(PacketListener packetListener) {
// Do nothing.
}
}
public class DummyServerGamePacketListenerImpl extends ServerGamePacketListenerImpl {
DummyServerGamePacketListenerImpl(MinecraftServer minecraftServer, ServerPlayer serverPlayer) {
super(minecraftServer, new DummyConnection(), serverPlayer,
CommonListenerCookie.createInitial(ChunkLoaderNPCImpl.this.getGameProfile(), false));
}
@Override
public void handleContainerClick(ServerboundContainerClickPacket containerClickPacket) {
// Do nothing.
}
@Override
public void handleMovePlayer(ServerboundMovePlayerPacket movePlayerPacket) {
// Do nothing.
}
@Override
public void handleSignUpdate(ServerboundSignUpdatePacket signUpdatePacket) {
// Do nothing.
}
@Override
public void handlePlayerAction(ServerboundPlayerActionPacket playerActionPacket) {
// Do nothing.
}
@Override
public void handleUseItem(ServerboundUseItemPacket useItemPacket) {
// Do nothing.
}
@Override
public void handleSetCarriedItem(ServerboundSetCarriedItemPacket setCarriedItemPacket) {
// Do nothing.
}
@Override
public void handleChat(ServerboundChatPacket chatPacket) {
// Do nothing.
}
@Override
public void disconnect(String s) {
// Do nothing.
}
public void send(Packet<?> packet) {
// Do nothing.
}
}
private static class DummyPlayerAdvancements extends PlayerAdvancements {
DummyPlayerAdvancements(MinecraftServer server, ServerPlayer serverPlayer) {
super(server.getFixerUpper(), server.getPlayerList(), server.getAdvancements(),
getAdvancementsFile(server, serverPlayer), serverPlayer);
}
private static Path getAdvancementsFile(MinecraftServer server, ServerPlayer serverPlayer) {
File advancementsDir = server.getWorldPath(LevelResource.PLAYER_ADVANCEMENTS_DIR).toFile();
return new File(advancementsDir, serverPlayer.getUUID() + ".json").toPath();
}
@Override
public void setPlayer(ServerPlayer owner) {
// Do nothing.
}
@Override
public void stopListening() {
// Do nothing.
}
@Override
public void reload(ServerAdvancementManager advancementLoader) {
// Do nothing.
}
@Override
public void save() {
// Do nothing.
}
@Override
public boolean award(AdvancementHolder advancement, String criterionName) {
return false;
}
@Override
public boolean revoke(AdvancementHolder advancement, String criterionName) {
return false;
}
@Override
public void flushDirty(ServerPlayer player) {
// Do nothing.
}
@Override
public void setSelectedTab(@Nullable AdvancementHolder advancement) {
// Do nothing.
}
@Override
public AdvancementProgress getOrStartProgress(AdvancementHolder advancement) {
return new AdvancementProgress();
}
}
}

View File

@ -0,0 +1,165 @@
package com.bgsoftware.wildloaders.nms.v1_20_4;
import com.bgsoftware.wildloaders.api.holograms.Hologram;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftArmorStand;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.util.CraftChatMessage;
public final class EntityHologram extends ArmorStand implements Hologram {
private static final AABB EMPTY_BOUND = new AABB(0D, 0D, 0D, 0D, 0D, 0D);
private CraftEntity bukkitEntity;
public EntityHologram(ServerLevel serverLevel, double x, double y, double z) {
super(serverLevel, x, y, z);
setInvisible(true);
setSmall(true);
setShowArms(false);
setNoGravity(true);
setNoBasePlate(true);
setMarker(true);
super.collides = false;
super.setCustomNameVisible(true); // Custom name visible
super.setBoundingBox(EMPTY_BOUND);
}
@Override
public void setHologramName(String name) {
super.setCustomName(CraftChatMessage.fromStringOrNull(name));
}
@Override
public void removeHologram() {
super.remove(RemovalReason.DISCARDED);
}
@Override
public org.bukkit.entity.Entity getEntity() {
return getBukkitEntity();
}
@Override
public void tick() {
// Disable normal ticking for this entity.
// Workaround to force EntityTrackerEntry to send a teleport packet immediately after spawning this entity.
if (this.onGround) {
this.onGround = false;
}
}
@Override
public void inactiveTick() {
// Disable normal ticking for this entity.
// Workaround to force EntityTrackerEntry to send a teleport packet immediately after spawning this entity.
if (this.onGround) {
this.onGround = false;
}
}
@Override
public void addAdditionalSaveData(CompoundTag compoundTag) {
// Do not save NBT.
}
@Override
public boolean saveAsPassenger(CompoundTag compoundTag) {
// Do not save NBT.
return false;
}
@Override
public CompoundTag saveWithoutId(CompoundTag compoundTag) {
// Do not save NBT.
return compoundTag;
}
@Override
public void readAdditionalSaveData(CompoundTag compoundTag) {
// Do not load NBT.
}
@Override
public void load(CompoundTag compoundTag) {
// Do not load NBT.
}
@Override
public boolean isInvulnerableTo(DamageSource source) {
/*
* The field Entity.invulnerable is private.
* It's only used while saving NBTTags, but since the entity would be killed
* on chunk unload, we prefer to override isInvulnerable().
*/
return true;
}
@Override
public boolean repositionEntityAfterLoad() {
return false;
}
@Override
public void setCustomName(Component component) {
// Locks the custom name.
}
@Override
public void setCustomNameVisible(boolean visible) {
// Locks the custom name.
}
@Override
public InteractionResult interactAt(Player player, Vec3 hitPos, InteractionHand hand) {
// Prevent stand being equipped
return InteractionResult.PASS;
}
@Override
public void setItemSlot(EquipmentSlot equipmentSlot, ItemStack itemStack, boolean silent) {
// Prevent stand being equipped
}
@Override
public AABB getBoundingBoxForCulling() {
return EMPTY_BOUND;
}
@Override
public void playSound(SoundEvent soundEvent, float volume, float pitch) {
// Remove sounds.
}
@Override
public void remove(RemovalReason removalReason) {
// Prevent being killed.
}
@Override
public CraftEntity getBukkitEntity() {
if (bukkitEntity == null) {
bukkitEntity = new CraftArmorStand((CraftServer) Bukkit.getServer(), this);
}
return bukkitEntity;
}
}

View File

@ -0,0 +1,176 @@
package com.bgsoftware.wildloaders.nms.v1_20_4;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_20_4.loader.ChunkLoaderBlockEntity;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.component.ResolvableProfile;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import java.util.Optional;
import java.util.UUID;
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String def) {
ItemStack itemStack = CraftItemStack.asNMSCopy(bukkitItem);
CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag compoundTag = customData.getUnsafe();
if (compoundTag.contains(key, 8))
return compoundTag.getString(key);
}
return def;
}
@Override
public org.bukkit.inventory.ItemStack setTag(org.bukkit.inventory.ItemStack bukkitItem, String key, String value) {
ItemStack itemStack = CraftItemStack.asNMSCopy(bukkitItem);
CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
customData = customData.update(compoundTag -> compoundTag.putString(key, value));
itemStack.set(DataComponents.CUSTOM_DATA, customData);
return CraftItemStack.asBukkitCopy(itemStack);
}
@Override
public long getTag(org.bukkit.inventory.ItemStack bukkitItem, String key, long def) {
ItemStack itemStack = CraftItemStack.asNMSCopy(bukkitItem);
CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag compoundTag = customData.getUnsafe();
if (compoundTag.contains(key, 4))
return compoundTag.getLong(key);
}
return def;
}
@Override
public org.bukkit.inventory.ItemStack setTag(org.bukkit.inventory.ItemStack bukkitItem, String key, long value) {
ItemStack itemStack = CraftItemStack.asNMSCopy(bukkitItem);
CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
customData = customData.update(compoundTag -> compoundTag.putLong(key, value));
itemStack.set(DataComponents.CUSTOM_DATA, customData);
return CraftItemStack.asBukkitCopy(itemStack);
}
@Override
public org.bukkit.inventory.ItemStack getPlayerSkull(org.bukkit.inventory.ItemStack bukkitItem, String texture) {
ItemStack itemStack = CraftItemStack.asNMSCopy(bukkitItem);
PropertyMap propertyMap = new PropertyMap();
propertyMap.put("textures", new Property("textures", texture));
ResolvableProfile resolvableProfile = new ResolvableProfile(Optional.empty(), Optional.empty(), propertyMap);
itemStack.set(DataComponents.PROFILE, resolvableProfile);
return CraftItemStack.asBukkitCopy(itemStack);
}
@Override
public com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC createNPC(Location location, UUID uuid) {
return new ChunkLoaderNPCImpl(((CraftServer) Bukkit.getServer()).getServer(), location, uuid);
}
@Override
public ITileEntityChunkLoader createLoader(ChunkLoader chunkLoader) {
Location loaderLoc = chunkLoader.getLocation();
World bukkitWorld = loaderLoc.getWorld();
if (bukkitWorld == null)
throw new IllegalArgumentException("Cannot create loader in null world.");
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(loaderLoc.getBlockX(), loaderLoc.getBlockY(), loaderLoc.getBlockZ());
ChunkLoaderBlockEntity ChunkLoaderBlockEntity = new ChunkLoaderBlockEntity(chunkLoader, serverLevel, blockPos);
serverLevel.addBlockEntityTicker(ChunkLoaderBlockEntity.getTicker());
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = -1;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, true);
}
return ChunkLoaderBlockEntity;
}
@Override
public void removeLoader(ChunkLoader chunkLoader, boolean spawnParticle) {
Location loaderLoc = chunkLoader.getLocation();
World bukkitWorld = loaderLoc.getWorld();
if (bukkitWorld == null)
throw new IllegalArgumentException("Cannot remove loader in null world.");
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(loaderLoc.getBlockX(), loaderLoc.getBlockY(), loaderLoc.getBlockZ());
long chunkPosLong = ChunkPos.asLong(blockPos.getX() >> 4, blockPos.getZ() >> 4);
ChunkLoaderBlockEntity chunkLoaderBlockEntity = ChunkLoaderBlockEntity.chunkLoaderBlockEntityMap.remove(chunkPosLong);
if (chunkLoaderBlockEntity != null) {
chunkLoaderBlockEntity.holograms.forEach(EntityHologram::removeHologram);
chunkLoaderBlockEntity.removed = true;
}
if (spawnParticle)
serverLevel.levelEvent(null, 2001, blockPos, Block.getId(serverLevel.getBlockState(blockPos)));
for (org.bukkit.Chunk bukkitChunk : chunkLoader.getLoadedChunks()) {
LevelChunk levelChunk = serverLevel.getChunk(bukkitChunk.getX(), bukkitChunk.getZ());
levelChunk.getBlockEntities().values().stream()
.filter(blockEntity -> blockEntity instanceof SpawnerBlockEntity)
.forEach(blockEntity -> {
((SpawnerBlockEntity) blockEntity).getSpawner().requiredPlayerRange = 16;
});
ChunkPos chunkPos = levelChunk.getPos();
serverLevel.setChunkForced(chunkPos.x, chunkPos.z, false);
}
}
@Override
public void updateSpawner(Location location, boolean reset) {
World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
throw new IllegalArgumentException("Cannot remove loader in null world.");
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockEntity blockEntity = serverLevel.getBlockEntity(blockPos);
if (blockEntity instanceof SpawnerBlockEntity spawnerBlockEntity)
spawnerBlockEntity.getSpawner().requiredPlayerRange = reset ? 16 : -1;
}
}

View File

@ -0,0 +1,149 @@
package com.bgsoftware.wildloaders.nms.v1_20_4.loader;
import com.bgsoftware.wildloaders.api.holograms.Hologram;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.loaders.WChunkLoader;
import com.bgsoftware.wildloaders.nms.v1_20_4.EntityHologram;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public final class ChunkLoaderBlockEntity extends BlockEntity implements ITileEntityChunkLoader {
public static final Map<Long, ChunkLoaderBlockEntity> chunkLoaderBlockEntityMap = new HashMap<>();
public final List<EntityHologram> holograms = new ArrayList<>();
private final WChunkLoader chunkLoader;
private final Block loaderBlock;
private final ChunkLoaderBlockEntityTicker ticker;
private final ServerLevel serverLevel;
private final BlockPos blockPos;
private final String cachedPlacerName;
private short currentTick = 20;
private short daysAmount, hoursAmount, minutesAmount, secondsAmount;
public boolean removed = false;
public ChunkLoaderBlockEntity(ChunkLoader chunkLoader, ServerLevel serverLevel, BlockPos blockPos) {
super(BlockEntityType.COMMAND_BLOCK, blockPos, serverLevel.getBlockState(blockPos));
this.chunkLoader = (WChunkLoader) chunkLoader;
this.ticker = new ChunkLoaderBlockEntityTicker(this);
this.blockPos = blockPos;
this.serverLevel = serverLevel;
setLevel(serverLevel);
loaderBlock = serverLevel.getBlockState(blockPos).getBlock();
this.cachedPlacerName = Optional.ofNullable(this.chunkLoader.getWhoPlaced().getName()).orElse("");
if (!this.chunkLoader.isInfinite()) {
long timeLeft = chunkLoader.getTimeLeft();
daysAmount = (short) (timeLeft / 86400);
timeLeft = timeLeft % 86400;
hoursAmount = (short) (timeLeft / 3600);
timeLeft = timeLeft % 3600;
minutesAmount = (short) (timeLeft / 60);
timeLeft = timeLeft % 60;
secondsAmount = (short) timeLeft;
}
long chunkPosLong = ChunkPos.asLong(blockPos.getX() >> 4, blockPos.getZ() >> 4);
chunkLoaderBlockEntityMap.put(chunkPosLong, this);
List<String> hologramLines = this.chunkLoader.getHologramLines();
double currentY = blockPos.getY() + 1;
for (int i = hologramLines.size(); i > 0; i--) {
EntityHologram hologram = new EntityHologram(serverLevel, blockPos.getX() + 0.5, currentY, blockPos.getZ() + 0.5);
updateName(hologram, hologramLines.get(i - 1));
serverLevel.addFreshEntity(hologram);
currentY += 0.23;
holograms.add(hologram);
}
}
public void tick() {
if (removed || ++currentTick <= 20)
return;
currentTick = 0;
if (chunkLoader.isNotActive() || this.serverLevel.getBlockState(this.blockPos).getBlock() != loaderBlock) {
chunkLoader.remove();
return;
}
if (chunkLoader.isInfinite())
return;
List<String> hologramLines = chunkLoader.getHologramLines();
int hologramsAmount = holograms.size();
for (int i = hologramsAmount; i > 0; i--) {
EntityHologram hologram = holograms.get(hologramsAmount - i);
updateName(hologram, hologramLines.get(i - 1));
}
chunkLoader.tick();
if (!removed) {
secondsAmount--;
if (secondsAmount < 0) {
secondsAmount = 59;
minutesAmount--;
if (minutesAmount < 0) {
minutesAmount = 59;
hoursAmount--;
if (hoursAmount < 0) {
hoursAmount = 23;
daysAmount--;
}
}
}
}
}
@Override
public Collection<Hologram> getHolograms() {
return Collections.unmodifiableList(holograms);
}
@Override
public boolean isRemoved() {
return removed || super.isRemoved();
}
public ChunkLoaderBlockEntityTicker getTicker() {
return ticker;
}
private void updateName(EntityHologram hologram, String line) {
hologram.setHologramName(line
.replace("{0}", this.cachedPlacerName)
.replace("{1}", daysAmount + "")
.replace("{2}", hoursAmount + "")
.replace("{3}", minutesAmount + "")
.replace("{4}", secondsAmount + "")
);
}
}

View File

@ -0,0 +1,30 @@
package com.bgsoftware.wildloaders.nms.v1_20_4.loader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
public record ChunkLoaderBlockEntityTicker(
ChunkLoaderBlockEntity chunkLoaderBlockEntity) implements TickingBlockEntity {
@Override
public void tick() {
chunkLoaderBlockEntity.tick();
}
@Override
public boolean isRemoved() {
return chunkLoaderBlockEntity.isRemoved();
}
@Override
public BlockPos getPos() {
return chunkLoaderBlockEntity.getBlockPos();
}
@Override
public String getType() {
return BlockEntityType.getKey(chunkLoaderBlockEntity.getType()) + "";
}
}

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_7_R4;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_7_R4.loader.TileEntityChunkLoader;
import net.minecraft.server.v1_7_R4.Block;
import net.minecraft.server.v1_7_R4.Chunk;
@ -21,7 +22,7 @@ import org.bukkit.craftbukkit.v1_7_R4.util.LongHash;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack itemStack, String key, String def) {

View File

@ -2,6 +2,7 @@ package com.bgsoftware.wildloaders.nms.v1_8_R3;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.loaders.ITileEntityChunkLoader;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.nms.v1_8_R3.loader.TileEntityChunkLoader;
import net.minecraft.server.v1_8_R3.Block;
import net.minecraft.server.v1_8_R3.BlockPosition;
@ -21,7 +22,7 @@ import org.bukkit.craftbukkit.v1_8_R3.util.LongHash;
import java.util.UUID;
public final class NMSAdapter implements com.bgsoftware.wildloaders.nms.NMSAdapter {
public final class NMSAdapterImpl implements NMSAdapter {
@Override
public String getTag(org.bukkit.inventory.ItemStack itemStack, String key, String def) {

View File

@ -1,6 +1,6 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.1.2'
id 'io.github.goooler.shadow' version '8.1.7'
id 'maven-publish'
}
@ -16,7 +16,13 @@ project.ext {
allprojects {
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'io.github.goooler.shadow'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
repositories {
maven { url 'https://repo.bg-software.com/repository/nms/' }
@ -26,7 +32,7 @@ allprojects {
}
dependencies {
compileOnly "com.bgsoftware.common.reflection:ReflectionUtils:latest"
compileOnly "com.bgsoftware.common.reflection:ReflectionUtils:b3"
}
task checkDebug() {
@ -51,13 +57,21 @@ subprojects {
}
}
repositories {
mavenCentral()
}
dependencies {
implementation fileTree(rootProject.archiveFolder.getAbsolutePath())
implementation project(":API")
implementation 'com.bgsoftware.common.reflection:ReflectionUtils:latest'
implementation 'com.bgsoftware.common.config:CommentedConfiguration:1.0.3'
implementation 'com.bgsoftware.common.reflection:ReflectionUtils:b3'
implementation 'com.bgsoftware.common.updater:Updater:b1'
implementation 'com.bgsoftware.common.config:CommentedConfiguration:b1'
implementation 'com.bgsoftware.common.dependencies:DependenciesManager:b2'
implementation 'com.bgsoftware.common.nmsloader:NMSLoader:b3'
implementation 'org.bstats:bstats-bukkit:3.0.0'
// Spigot jars
compileOnly "org.spigotmc:v1_8_R3:latest"
@ -86,6 +100,9 @@ processResources {
shadowJar {
dependsOn(jar)
relocate 'org.bstats', 'com.bgsoftware.wildloaders.libs.org.bstats'
relocate 'com.bgsoftware.common', 'com.bgsoftware.wildloaders.libs.com.bgsoftware.common'
archiveFileName = rootProject.name + "-" + rootProject.buildVersion + ".jar"
delete fileTree(rootProject.targetFolder.getAbsolutePath())
@ -116,3 +133,6 @@ build {
publish.shouldRunAfter shadowJar
shadowJar.shouldRunAfter build
build.shouldRunAfter subprojects.build
shadowJar.dependsOn subprojects.build
compileJava.dependsOn childProjects.values().shadowJar

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -28,3 +28,5 @@ include 'NMS:v1_19'
include 'NMS:v1_20_1'
include 'NMS:v1_20_2'
include 'NMS:v1_20_3'
include 'NMS:v1_20_4'

View File

@ -1,71 +0,0 @@
package com.bgsoftware.wildloaders;
import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
public final class Updater {
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private static String latestVersion, versionDescription;
static{
setLatestVersion();
}
//Just so no one would be able to call the constructor
private Updater(){}
public static boolean isOutdated(){
return !plugin.getDescription().getVersion().startsWith(latestVersion);
}
public static String getLatestVersion(){
return latestVersion;
}
static String getVersionDescription(){
return versionDescription;
}
private static void setLatestVersion(){
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://bg-software.com/versions.json").openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
connection.setDoInput(true);
try(InputStream reader = connection.getInputStream()){
Class<?> jsonObjectClass, gsonClass;
try{
jsonObjectClass = Class.forName("net.minecraft.util.com.google.gson.JsonObject");
gsonClass = Class.forName("net.minecraft.util.com.google.gson.Gson");
}catch(ClassNotFoundException ex){
jsonObjectClass = Class.forName("com.google.gson.JsonObject");
gsonClass = Class.forName("com.google.gson.Gson");
}
Object jsonObject = gsonClass.getMethod("fromJson", Reader.class, Class.class)
.invoke(gsonClass.newInstance(), new InputStreamReader(reader), jsonObjectClass);
Object jsonElement = jsonObjectClass.getMethod("get", String.class).invoke(jsonObject, "wildloaders");
Object plugin = jsonElement.getClass().getMethod("getAsJsonObject").invoke(jsonElement);
Object versionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "version");
Object descriptionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "description");
latestVersion = (String) versionElement.getClass().getMethod("getAsString").invoke(versionElement);
versionDescription = (String) descriptionElement.getClass().getMethod("getAsString").invoke(descriptionElement);
}
} catch(Exception ex){
//Something went wrong...
latestVersion = plugin.getDescription().getVersion();
}
}
}

View File

@ -1,7 +1,10 @@
package com.bgsoftware.wildloaders;
import com.bgsoftware.common.dependencies.DependenciesManager;
import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.common.nmsloader.INMSLoader;
import com.bgsoftware.common.nmsloader.NMSHandlersFactory;
import com.bgsoftware.common.nmsloader.NMSLoadException;
import com.bgsoftware.common.updater.Updater;
import com.bgsoftware.wildloaders.api.WildLoaders;
import com.bgsoftware.wildloaders.api.WildLoadersAPI;
import com.bgsoftware.wildloaders.command.CommandsHandler;
@ -13,21 +16,18 @@ import com.bgsoftware.wildloaders.handlers.SettingsHandler;
import com.bgsoftware.wildloaders.listeners.BlocksListener;
import com.bgsoftware.wildloaders.listeners.ChunksListener;
import com.bgsoftware.wildloaders.listeners.PlayersListener;
import com.bgsoftware.wildloaders.metrics.Metrics;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.utils.Pair;
import com.bgsoftware.wildloaders.utils.ServerVersion;
import com.bgsoftware.wildloaders.utils.database.Database;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.UnsafeValues;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
private final Updater updater = new Updater(this, "wildloaders");
private static WildLoadersPlugin plugin;
private SettingsHandler settingsHandler;
@ -46,7 +46,7 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
DependenciesManager.inject(this);
new Metrics(this);
new Metrics(this, 21732);
shouldEnable = loadNMSAdapter();
loadAPI();
@ -80,10 +80,10 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
Locale.reload();
if (Updater.isOutdated()) {
if (updater.isOutdated()) {
log("");
log("A new version is available (v" + Updater.getLatestVersion() + ")!");
log("Version's description: \"" + Updater.getVersionDescription() + "\"");
log("A new version is available (v" + updater.getLatestVersion() + ")!");
log("Version's description: \"" + updater.getVersionDescription() + "\"");
log("");
}
@ -100,52 +100,19 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
}
private boolean loadNMSAdapter() {
String version = null;
if (ServerVersion.isLessThan(ServerVersion.v1_17)) {
version = getServer().getClass().getPackage().getName().split("\\.")[3];
} else {
ReflectMethod<Integer> getDataVersion = new ReflectMethod<>(UnsafeValues.class, "getDataVersion");
int dataVersion = getDataVersion.invoke(Bukkit.getUnsafe());
List<Pair<Integer, String>> versions = Arrays.asList(
new Pair<>(2729, null),
new Pair<>(2730, "v1_17"),
new Pair<>(2974, null),
new Pair<>(2975, "v1_18"),
new Pair<>(3336, null),
new Pair<>(3337, "v1_19"),
new Pair<>(3465, "v1_20_1"),
new Pair<>(3578, "v1_20_2"),
new Pair<>(3700, "v1_20_3")
);
for (Pair<Integer, String> versionData : versions) {
if (dataVersion <= versionData.first) {
version = versionData.second;
break;
}
}
if (version == null) {
log("Data version: " + dataVersion);
}
}
if (version != null) {
try {
nmsAdapter = (NMSAdapter) Class.forName(String.format("com.bgsoftware.wildloaders.nms.%s.NMSAdapter", version)).newInstance();
return true;
} catch (Exception error) {
error.printStackTrace();
}
}
INMSLoader nmsLoader = NMSHandlersFactory.createNMSLoader(this);
this.nmsAdapter = nmsLoader.loadNMSHandler(NMSAdapter.class);
return true;
} catch (NMSLoadException error) {
log("&cThe plugin doesn't support your minecraft version.");
log("&cPlease try a different version.");
error.printStackTrace();
return false;
}
}
private void loadAPI() {
try {
@ -186,6 +153,10 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
return dataHandler;
}
public Updater getUpdater() {
return updater;
}
public static void log(String message) {
plugin.getLogger().info(message);
}

View File

@ -1,6 +1,5 @@
package com.bgsoftware.wildloaders.listeners;
import com.bgsoftware.wildloaders.Updater;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC;
import com.bgsoftware.wildloaders.utils.threads.Executor;
@ -37,9 +36,9 @@ public final class PlayersListener implements Listener {
ChatColor.GRAY + "This server is using WildLoaders v" + plugin.getDescription().getVersion()), 5L);
}
if (e.getPlayer().isOp() && Updater.isOutdated()) {
if (e.getPlayer().isOp() && plugin.getUpdater().isOutdated()) {
Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "WildLoaders" +
ChatColor.GRAY + " A new version is available (v" + Updater.getLatestVersion() + ")!"), 20L);
ChatColor.GRAY + " A new version is available (v" + plugin.getUpdater().getLatestVersion() + ")!"), 20L);
}
}

View File

@ -1,707 +0,0 @@
package com.bgsoftware.wildloaders.metrics;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.zip.GZIPOutputStream;
/**
* bStats collects some data for plugin authors.
* <p>
* Check out https://bStats.org/ to learn more about bStats!
*/
@SuppressWarnings("all")
public class Metrics {
static {
// You can use the property to disable the check in your test environment
if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) {
// Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
final String defaultPackage = new String(
new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'});
final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
// We want to make sure nobody just copy & pastes the example and use the wrong package names
if (Metrics.class.getPackage().getName().equals(defaultPackage) || Metrics.class.getPackage().getName().equals(examplePackage)) {
throw new IllegalStateException("bStats Metrics class has not been relocated correctly!");
}
}
}
// The version of this bStats class
public static final int B_STATS_VERSION = 1;
// The url to which the data is sent
private static final String URL = "https://bStats.org/submitData/bukkit";
// Is bStats enabled on this server?
private boolean enabled;
// Should failed requests be logged?
private static boolean logFailedRequests;
// Should the sent data be logged?
private static boolean logSentData;
// Should the response text be logged?
private static boolean logResponseStatusText;
// The uuid of the server
private static String serverUUID;
// The plugin
private final Plugin plugin;
// A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>();
/**
* Class constructor.
*
* @param plugin The plugin which stats should be submitted.
*/
public Metrics(Plugin plugin) {
if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null!");
}
this.plugin = plugin;
// Get the config file
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
File configFile = new File(bStatsFolder, "config.yml");
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
// Check if the config file exists
if (!config.isSet("serverUuid")) {
// Add default values
config.addDefault("enabled", true);
// Every server gets it's unique random id.
config.addDefault("serverUuid", UUID.randomUUID().toString());
// Should failed request be logged?
config.addDefault("logFailedRequests", false);
// Should the sent data be logged?
config.addDefault("logSentData", false);
// Should the response text be logged?
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats
config.options().header(
"bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
"To honor their work, you should not disable it.\n" +
"This has nearly no effect on the server performance!\n" +
"Check out https://bStats.org/ to learn more :)"
).copyDefaults(true);
try {
config.save(configFile);
} catch (IOException ignored) { }
}
// Load the data
enabled = config.getBoolean("enabled", true);
serverUUID = config.getString("serverUuid");
logFailedRequests = config.getBoolean("logFailedRequests", false);
logSentData = config.getBoolean("logSentData", false);
logResponseStatusText = config.getBoolean("logResponseStatusText", false);
if (enabled) {
boolean found = false;
// Search for all other bStats Metrics classes to see if we are the first one
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try {
service.getField("B_STATS_VERSION"); // Our identifier :)
found = true; // We aren't the first
break;
} catch (NoSuchFieldException ignored) { }
}
// Register our service
Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal);
if (!found) {
// We are the first!
startSubmitting();
}
}
}
/**
* Checks if bStats is enabled.
*
* @return Whether bStats is enabled or not.
*/
public boolean isEnabled() {
return enabled;
}
/**
* Adds a custom chart.
*
* @param chart The chart to add.
*/
public void addCustomChart(CustomChart chart) {
if (chart == null) {
throw new IllegalArgumentException("Chart cannot be null!");
}
charts.add(chart);
}
/**
* Starts the Scheduler which submits our data every 30 minutes.
*/
private void startSubmitting() {
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!plugin.isEnabled()) { // Plugin was disabled
timer.cancel();
return;
}
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, () -> submitData());
}
}, 1000 * 60 * 5, 1000 * 60 * 30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it!
}
/**
* Gets the plugin specific data.
* This method is called using Reflection.
*
* @return The plugin specific data.
*/
public JSONObject getPluginData() {
JSONObject data = new JSONObject();
String pluginName = plugin.getDescription().getName();
String pluginVersion = plugin.getDescription().getVersion();
data.put("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", pluginVersion); // Append the version of the plugin
JSONArray customCharts = new JSONArray();
for (CustomChart customChart : charts) {
// Add the data of the custom charts
JSONObject chart = customChart.getRequestJsonObject();
if (chart == null) { // If the chart is null, we skip it
continue;
}
customCharts.add(chart);
}
data.put("customCharts", customCharts);
return data;
}
/**
* Gets the server specific data.
*
* @return The server specific data.
*/
private JSONObject getServerData() {
// Minecraft specific data
int playerAmount;
try {
// Around MC 1.8 the return type was changed to a collection from an array,
// This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class)
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
} catch (Exception e) {
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
}
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
String bukkitVersion = Bukkit.getVersion();
// OS/Java specific data
String javaVersion = System.getProperty("java.version");
String osName = System.getProperty("os.name");
String osArch = System.getProperty("os.arch");
String osVersion = System.getProperty("os.version");
int coreCount = Runtime.getRuntime().availableProcessors();
JSONObject data = new JSONObject();
data.put("serverUUID", serverUUID);
data.put("playerAmount", playerAmount);
data.put("onlineMode", onlineMode);
data.put("bukkitVersion", bukkitVersion);
data.put("javaVersion", javaVersion);
data.put("osName", osName);
data.put("osArch", osArch);
data.put("osVersion", osVersion);
data.put("coreCount", coreCount);
return data;
}
/**
* Collects the data and sends it afterwards.
*/
private void submitData() {
final JSONObject data = getServerData();
JSONArray pluginData = new JSONArray();
// Search for all other bStats Metrics classes to get their plugin data
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try {
service.getField("B_STATS_VERSION"); // Our identifier :)
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) {
try {
pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider()));
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
}
} catch (NoSuchFieldException ignored) { }
}
data.put("plugins", pluginData);
// Create a new thread for the connection to the bStats server
new Thread(new Runnable() {
@Override
public void run() {
try {
// Send the data
sendData(plugin, data);
} catch (Exception e) {
// Something went wrong! :(
if (logFailedRequests) {
plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
}
}
}
}).start();
}
/**
* Sends the data to the bStats server.
*
* @param plugin Any plugin. It's just used to get a logger instance.
* @param data The data to send.
* @throws Exception If the request failed.
*/
private static void sendData(Plugin plugin, JSONObject data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("Data cannot be null!");
}
if (Bukkit.isPrimaryThread()) {
throw new IllegalAccessException("This method must not be called from the main thread!");
}
if (logSentData) {
plugin.getLogger().info("Sending data to bStats: " + data.toString());
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth
byte[] compressedData = compress(data.toString());
// Add headers
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
// Send data
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(compressedData);
outputStream.flush();
outputStream.close();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
bufferedReader.close();
if (logResponseStatusText) {
plugin.getLogger().info("Sent data to bStats and received response: " + builder.toString());
}
}
/**
* Gzips the given String.
*
* @param str The string to gzip.
* @return The gzipped String.
* @throws IOException If the compression failed.
*/
private static byte[] compress(final String str) throws IOException {
if (str == null) {
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close();
return outputStream.toByteArray();
}
/**
* Represents a custom chart.
*/
public static abstract class CustomChart {
// The id of the chart
final String chartId;
/**
* Class constructor.
*
* @param chartId The id of the chart.
*/
CustomChart(String chartId) {
if (chartId == null || chartId.isEmpty()) {
throw new IllegalArgumentException("ChartId cannot be null or empty!");
}
this.chartId = chartId;
}
private JSONObject getRequestJsonObject() {
JSONObject chart = new JSONObject();
chart.put("chartId", chartId);
try {
JSONObject data = getChartData();
if (data == null) {
// If the data is null we don't send the chart.
return null;
}
chart.put("data", data);
} catch (Throwable t) {
if (logFailedRequests) {
Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
}
return null;
}
return chart;
}
protected abstract JSONObject getChartData() throws Exception;
}
/**
* Represents a custom simple pie.
*/
public static class SimplePie extends CustomChart {
private final Callable<String> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimplePie(String chartId, Callable<String> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
String value = callable.call();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
}
data.put("value", value);
return data;
}
}
/**
* Represents a custom advanced pie.
*/
public static class AdvancedPie extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
continue; // Skip this invalid
}
allSkipped = false;
values.put(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
return data;
}
}
/**
* Represents a custom drilldown pie.
*/
public static class DrilldownPie extends CustomChart {
private final Callable<Map<String, Map<String, Integer>>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
super(chartId);
this.callable = callable;
}
@Override
public JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JSONObject value = new JSONObject();
boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
value.put(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false;
}
if (!allSkipped) {
reallyAllSkipped = false;
values.put(entryValues.getKey(), value);
}
}
if (reallyAllSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
return data;
}
}
/**
* Represents a custom single line chart.
*/
public static class SingleLineChart extends CustomChart {
private final Callable<Integer> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SingleLineChart(String chartId, Callable<Integer> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
int value = callable.call();
if (value == 0) {
// Null = skip the chart
return null;
}
data.put("value", value);
return data;
}
}
/**
* Represents a custom multi line chart.
*/
public static class MultiLineChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() == 0) {
continue; // Skip this invalid
}
allSkipped = false;
values.put(entry.getKey(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
return data;
}
}
/**
* Represents a custom simple bar chart.
*/
public static class SimpleBarChart extends CustomChart {
private final Callable<Map<String, Integer>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
JSONArray categoryValues = new JSONArray();
categoryValues.add(entry.getValue());
values.put(entry.getKey(), categoryValues);
}
data.put("values", values);
return data;
}
}
/**
* Represents a custom advanced bar chart.
*/
public static class AdvancedBarChart extends CustomChart {
private final Callable<Map<String, int[]>> callable;
/**
* Class constructor.
*
* @param chartId The id of the chart.
* @param callable The callable which is used to request the chart data.
*/
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
super(chartId);
this.callable = callable;
}
@Override
protected JSONObject getChartData() throws Exception {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
boolean allSkipped = true;
for (Map.Entry<String, int[]> entry : map.entrySet()) {
if (entry.getValue().length == 0) {
continue; // Skip this invalid
}
allSkipped = false;
JSONArray categoryValues = new JSONArray();
for (int categoryValue : entry.getValue()) {
categoryValues.add(categoryValue);
}
values.put(entry.getKey(), categoryValues);
}
if (allSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
return data;
}
}
}

View File

@ -24,9 +24,9 @@ public enum ServerVersion {
private static final boolean legacy;
static {
bukkitVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
String[] sections = bukkitVersion.split("_");
currentVersion = ServerVersion.valueOf(sections[0] + "_" + sections[1]);
bukkitVersion = Bukkit.getBukkitVersion().split("-")[0];
String[] sections = bukkitVersion.split("\\.");
currentVersion = ServerVersion.valueOf("v" + sections[0] + "_" + sections[1]);
legacy = isLessThan(ServerVersion.v1_13);
}