9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2026-01-04 15:41:45 +00:00

Delay Option + Removal of all NMS in common module

This commit is contained in:
LoJoSho
2022-12-21 17:44:49 -06:00
parent 98b861a830
commit 81f41a3e0c
11 changed files with 103 additions and 70 deletions

View File

@@ -1,12 +1,8 @@
plugins {
id("java")
id("com.github.johnrengelman.shadow") apply false
id("io.papermc.paperweight.userdev") version "1.3.8"
}
dependencies {
paperDevBundle("1.19.2-R0.1-SNAPSHOT")
compileOnly("com.mojang:authlib:1.5.25")
compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
compileOnly("org.jetbrains:annotations:23.0.0")
@@ -14,7 +10,7 @@ dependencies {
compileOnly("me.clip:placeholderapi:2.11.1")
compileOnly("com.ticxo.modelengine:api:R3.0.1")
compileOnly("com.github.oraxen:oraxen:-SNAPSHOT")
compileOnly("com.github.LoneDev6:API-ItemsAdder:3.2.5") // TODO Work on this
compileOnly("com.github.LoneDev6:API-ItemsAdder:3.2.5")
compileOnly("com.hibiscus:hmccolor:0.1-SNAPSHOT")
//compileOnly("com.github.Fisher2911:FisherLib:master-SNAPSHOT")
@@ -27,9 +23,7 @@ dependencies {
}
tasks {
build {
dependsOn(reobfJar)
}
}
java {

View File

@@ -13,6 +13,9 @@ public class DatabaseSettings {
private static final String MYSQL_HOST = "host";
private static final String MYSQL_USER = "user";
private static final String MYSQL_PORT = "port";
private static final String DELAY_PATH = "delay";
private static final String ENABLE_DELAY = "enabled";
private static final String DELAY_LENGTH = "delay";
private static String databaseType;
private static String database;
@@ -20,6 +23,8 @@ public class DatabaseSettings {
private static String host;
private static String username;
private static int port;
private static boolean enabledDelay;
private static int delayLength;
public static void load(ConfigurationNode source) {
//ConfigurationNode databaseSettings = source.node(DATABASE_SETTINGS_PATH);
@@ -33,6 +38,11 @@ public class DatabaseSettings {
host = mySql.node(MYSQL_HOST).getString();
username = mySql.node(MYSQL_USER).getString();
port = mySql.node(MYSQL_PORT).getInt();
ConfigurationNode delay = source.node(DELAY_PATH);
enabledDelay = delay.node(ENABLE_DELAY).getBoolean(false);
delayLength = delay.node(DELAY_LENGTH).getInt(2);
}
public static String getDatabaseType() {
@@ -58,4 +68,12 @@ public class DatabaseSettings {
public static int getPort() {
return port;
}
public static boolean isEnabledDelay() {
return enabledDelay;
}
public static int getDelayLength() {
return delayLength;
}
}

View File

@@ -1,6 +1,7 @@
package com.hibiscusmc.hmccosmetics.listener;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import com.hibiscusmc.hmccosmetics.config.DatabaseSettings;
import com.hibiscusmc.hmccosmetics.database.Database;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
@@ -11,16 +12,25 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerConnectionListener implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
CosmeticUser user = Database.get(event.getPlayer().getUniqueId());
CosmeticUsers.addUser(user);
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> {
user.updateCosmetic();
}, 2);
Runnable run = () -> {
CosmeticUser user = Database.get(event.getPlayer().getUniqueId());
CosmeticUsers.addUser(user);
HMCCosmeticsPlugin.getInstance().getLogger().info("Run User Join");
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> user.updateCosmetic(), 2);
};
if (DatabaseSettings.isEnabledDelay()) {
HMCCosmeticsPlugin.getInstance().getLogger().info("Delay Enabled with " + DatabaseSettings.getDelayLength() + " ticks");
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), run, DatabaseSettings.getDelayLength());
} else {
run.run();
}
}
@EventHandler

View File

@@ -1,14 +1,16 @@
package com.hibiscusmc.hmccosmetics.nms;
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType;
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType;
import com.hibiscusmc.hmccosmetics.entities.BalloonEntity;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import net.minecraft.network.protocol.Packet;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.List;
public interface NMSHandler {
int getNextEntityId();
@@ -23,7 +25,12 @@ public interface NMSHandler {
BalloonEntity spawnBalloon(CosmeticUser user, CosmeticBalloonType cosmeticBalloonType);
void sendPacket(Player player, Packet packet);
void equipmentSlotUpdate(
int entityId,
CosmeticUser user,
CosmeticSlot cosmeticSlot,
List<Player> sendTo
);
default boolean getSupported () {
return false;

View File

@@ -227,7 +227,6 @@ public class CosmeticUser {
if (invisibleArmorstand == null) return;
invisibleArmorstand.setHealth(0);
invisibleArmorstand.remove();
//invisibleArmorstand.remove(net.minecraft.world.entity.Entity.RemovalReason.DISCARDED);
this.invisibleArmorstand = null;
}

View File

@@ -2,9 +2,8 @@ package com.hibiscusmc.hmccosmetics.user;
import com.google.common.collect.HashBiMap;
import com.hibiscusmc.hmccosmetics.util.ServerUtils;
import net.minecraft.world.entity.EntityType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

View File

@@ -2,16 +2,10 @@ package com.hibiscusmc.hmccosmetics.util.packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
import net.minecraft.network.protocol.Packet;
import org.bukkit.entity.Player;
public class BasePacket {
public static void sendPacket(Player player, Packet<?> packet) {
NMSHandlers.getHandler().sendPacket(player, packet);
}
public static void sendPacket(Player player, PacketContainer packet) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
}

View File

@@ -5,28 +5,17 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.*;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticArmorType;
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
import com.hibiscusmc.hmccosmetics.util.InventoryUtils;
import com.hibiscusmc.hmccosmetics.util.PlayerUtils;
import com.hibiscusmc.hmccosmetics.util.packets.wrappers.WrapperPlayServerNamedEntitySpawn;
import com.hibiscusmc.hmccosmetics.util.packets.wrappers.WrapperPlayServerPlayerInfo;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_19_R1.CraftEquipmentSlot;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@@ -55,7 +44,11 @@ public class PacketManager extends BasePacket {
Player player,
int gamemode
) {
sendPacket(player, new ClientboundGameEventPacket(ClientboundGameEventPacket.CHANGE_GAME_MODE, (float) gamemode));
PacketContainer packet = new PacketContainer(PacketType.Play.Server.GAME_STATE_CHANGE);
packet.getGameStateIDs().write(0, 3);
// Tells what event this is. This is a change gamemode event.
packet.getFloat().write(0, (float) gamemode);
sendPacket(player, packet);
HMCCosmeticsPlugin.getInstance().getLogger().info("Gamemode Change sent to " + player + " to be " + gamemode);
}
@@ -92,38 +85,10 @@ public class PacketManager extends BasePacket {
CosmeticSlot cosmeticSlot,
List<Player> sendTo
) {
EquipmentSlot nmsSlot = null;
ItemStack nmsItem = null;
if (cosmeticSlot == CosmeticSlot.BACKPACK || cosmeticSlot == CosmeticSlot.BALLOON) return;
if (!(user.getCosmetic(cosmeticSlot) instanceof CosmeticArmorType)) {
NMSHandlers.getHandler().equipmentSlotUpdate(entityId, user, cosmeticSlot, sendTo);
nmsSlot = CraftEquipmentSlot.getNMS(InventoryUtils.getEquipmentSlot(cosmeticSlot));
nmsItem = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.AIR));
Pair<EquipmentSlot, ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
List<Pair<EquipmentSlot, ItemStack>> pairs = Collections.singletonList(pair);
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
return;
}
CosmeticArmorType cosmeticArmor = (CosmeticArmorType) user.getCosmetic(cosmeticSlot);
// Converting EquipmentSlot and ItemStack to NMS ones.
nmsSlot = CraftEquipmentSlot.getNMS(cosmeticArmor.getEquipSlot());
nmsItem = CraftItemStack.asNMSCopy(user.getUserCosmeticItem(cosmeticArmor));
if (nmsSlot == null) return;
Pair<EquipmentSlot, ItemStack> pair = new Pair<>(nmsSlot, nmsItem);
List<Pair<EquipmentSlot, ItemStack>> pairs = Collections.singletonList(pair);
ClientboundSetEquipmentPacket packet = new ClientboundSetEquipmentPacket(entityId, pairs);
for (Player p : sendTo) sendPacket(p, packet);
}
public static void armorStandMetaPacket(
@@ -230,7 +195,7 @@ public class PacketManager extends BasePacket {
*/
public static void sendEntityDestroyPacket(final int entityId, List<Player> sendTo) {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
packet.getModifier().write(0, new IntArrayList(new int[]{entityId}));
packet.getModifier().write(0, new int[]{entityId});
for (final Player p : sendTo) sendPacket(p, packet);
}

View File

@@ -12,6 +12,9 @@ database-settings:
port: 3306
host: localhost
user: username
delay:
enabled: false # This is for if other plugins need to handle certain aspects of a player first.
delay: 20 # In ticks
cosmetic-settings:
require-empty-helmet: false
require-empty-off-hand: false