mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-30 04:19:28 +00:00
WG Support + Fixes + Initial Mainhand Support
This commit is contained in:
@@ -7,20 +7,18 @@ import com.hibiscusmc.hmccosmetics.config.Settings;
|
||||
import com.hibiscusmc.hmccosmetics.config.WardrobeSettings;
|
||||
import com.hibiscusmc.hmccosmetics.config.serializer.ItemSerializer;
|
||||
import com.hibiscusmc.hmccosmetics.config.serializer.LocationSerializer;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics;
|
||||
import com.hibiscusmc.hmccosmetics.database.Database;
|
||||
import com.hibiscusmc.hmccosmetics.gui.Menus;
|
||||
import com.hibiscusmc.hmccosmetics.hooks.PAPIHook;
|
||||
import com.hibiscusmc.hmccosmetics.hooks.worldguard.WGHook;
|
||||
import com.hibiscusmc.hmccosmetics.hooks.items.ItemHooks;
|
||||
import com.hibiscusmc.hmccosmetics.hooks.worldguard.WGListener;
|
||||
import com.hibiscusmc.hmccosmetics.listener.PlayerConnectionListener;
|
||||
import com.hibiscusmc.hmccosmetics.listener.PlayerGameListener;
|
||||
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||
import com.hibiscusmc.hmccosmetics.util.misc.Translation;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -74,6 +72,17 @@ public final class HMCCosmeticsPlugin extends JavaPlugin {
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
new PAPIHook().register();
|
||||
}
|
||||
if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) {
|
||||
getServer().getPluginManager().registerEvents(new WGListener(), this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
// WorldGuard
|
||||
if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) {
|
||||
new WGHook();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@ public enum CosmeticSlot {
|
||||
CHESTPLATE,
|
||||
LEGGINGS,
|
||||
BOOTS,
|
||||
MAINHAND,
|
||||
OFFHAND,
|
||||
BACKPACK,
|
||||
BALLOON
|
||||
|
||||
@@ -41,7 +41,6 @@ public class CosmeticBalloonType extends Cosmetic {
|
||||
viewer.add(player);
|
||||
|
||||
PacketManager.sendTeleportPacket(user.getBalloonEntity().getPufferfishBalloonId(), actual, false, viewer);
|
||||
PacketManager.sendLeashPacket(user.getBalloonEntity().getPufferfishBalloonId(), player.getEntityId(), viewer);
|
||||
}
|
||||
|
||||
public String getModelName() {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.hibiscusmc.hmccosmetics.cosmetic.types;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.config.serializer.ItemSerializer;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.util.packets.PacketManager;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
|
||||
public class CosmeticMainhandType extends Cosmetic {
|
||||
|
||||
private ItemStack itemStack;
|
||||
|
||||
public CosmeticMainhandType(String id, ConfigurationNode config) {
|
||||
super(id, config);
|
||||
|
||||
this.itemStack = generateItemStack(config.node("item"));
|
||||
|
||||
setEquipable(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(CosmeticUser user) {
|
||||
Player player = user.getPlayer();
|
||||
|
||||
}
|
||||
|
||||
private ItemStack generateItemStack(ConfigurationNode config) {
|
||||
try {
|
||||
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);
|
||||
if (item == null) {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("Unable to create item for " + getId());
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
return item;
|
||||
} catch (SerializationException e) {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("Fatal error encountered for " + getId() + " regarding Serialization of item");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
return itemStack;
|
||||
}
|
||||
}
|
||||
@@ -68,10 +68,12 @@ public class BalloonEntity {
|
||||
final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId());
|
||||
if (model == null) {
|
||||
spawnModel(id, color);
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("model is null");
|
||||
return;
|
||||
}
|
||||
if (model.getRangeManager().getPlayerInRange().contains(player)) return;
|
||||
//if (model.getRangeManager().getPlayerInRange().contains(player)) return;
|
||||
model.showToPlayer(player);
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("Show to player");
|
||||
}
|
||||
public void removePlayerFromModel(final Player player) {
|
||||
final ModeledEntity model = ModelEngineAPI.api.getModeledEntity(modelEntity.getUniqueId());
|
||||
@@ -79,6 +81,7 @@ public class BalloonEntity {
|
||||
if (model == null) return;
|
||||
|
||||
model.hideFromPlayer(player);
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("Hidden from player");
|
||||
}
|
||||
|
||||
public Entity getModelEntity() {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.hibiscusmc.hmccosmetics.hooks.worldguard;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.protection.flags.Flag;
|
||||
import com.sk89q.worldguard.protection.flags.StateFlag;
|
||||
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
|
||||
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
|
||||
|
||||
public class WGHook {
|
||||
|
||||
public static StateFlag COSMETIC_ENABLE_FLAG;
|
||||
|
||||
public WGHook() {
|
||||
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
|
||||
try {
|
||||
StateFlag flag = new StateFlag("cosmetic-enable", false);
|
||||
registry.register(flag);
|
||||
COSMETIC_ENABLE_FLAG = flag; // only set
|
||||
} catch (FlagConflictException e) {
|
||||
Flag<?> existing = registry.get("cosmetic-enable");
|
||||
if (existing instanceof StateFlag) {
|
||||
COSMETIC_ENABLE_FLAG = (StateFlag) existing;
|
||||
} else {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("WorldGuard Unable to be hooked!");
|
||||
// types don't match - this is bad news! some other plugin conflicts with you
|
||||
// hopefully this never actually happens
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static StateFlag getCosmeticEnableFlag() {
|
||||
return COSMETIC_ENABLE_FLAG;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.hibiscusmc.hmccosmetics.hooks.worldguard;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.protection.ApplicableRegionSet;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import com.sk89q.worldguard.protection.regions.RegionContainer;
|
||||
import com.sk89q.worldguard.protection.regions.RegionQuery;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
public class WGListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
Location location = event.getPlayer().getLocation();
|
||||
com.sk89q.worldedit.util.Location loc = BukkitAdapter.adapt(location);
|
||||
RegionContainer region = WorldGuard.getInstance().getPlatform().getRegionContainer();
|
||||
RegionQuery query = region.createQuery();
|
||||
ApplicableRegionSet set = query.getApplicableRegions(loc);
|
||||
// TODO: Add more cosmetics
|
||||
CosmeticUser user = CosmeticUsers.getUser(event.getPlayer());
|
||||
if (set.getRegions().size() == 0) {
|
||||
user.showCosmetics();
|
||||
}
|
||||
for (ProtectedRegion protectedRegion : set.getRegions()) {
|
||||
if (protectedRegion.getFlags().containsKey(WGHook.getCosmeticEnableFlag())) {
|
||||
user.hideCosmetics();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,6 @@ public class PlayerConnectionListener implements Listener {
|
||||
for (Entity entity : event.getPlayer().getPassengers()) {
|
||||
if (entity.getType() == EntityType.ARMOR_STAND) {
|
||||
entity.remove();
|
||||
entity.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,7 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.player.PlayerItemDamageEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.player.PlayerToggleSneakEvent;
|
||||
import org.bukkit.event.player.*;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
@@ -125,7 +122,9 @@ public class PlayerGameListener implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerArmorDamage(PlayerItemDamageEvent event) {
|
||||
// Possibly look into cancelling the event, then handling the damage on our own.
|
||||
|
||||
if (event.isCancelled()) return;
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("PlayerItemDamageEvent");
|
||||
|
||||
int slot = -1;
|
||||
int w = 36;
|
||||
@@ -143,7 +142,10 @@ public class PlayerGameListener implements Listener {
|
||||
CosmeticUser user = CosmeticUsers.getUser(event.getPlayer().getUniqueId());
|
||||
CosmeticSlot cosmeticSlot = InventoryUtils.BukkitCosmeticSlot(slot);
|
||||
|
||||
if (!user.hasCosmeticInSlot(cosmeticSlot)) return;
|
||||
if (!user.hasCosmeticInSlot(cosmeticSlot)) {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("No cosmetic in " + cosmeticSlot);
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("PlayerItemDamageEvent UpdateCosmetic " + cosmeticSlot);
|
||||
@@ -151,6 +153,14 @@ public class PlayerGameListener implements Listener {
|
||||
}, 2);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onMainHandSwitch(PlayerChangedMainHandEvent event) {
|
||||
CosmeticUser user = CosmeticUsers.getUser(event.getPlayer().getUniqueId());
|
||||
|
||||
// TODO: Mainhand cosmetics
|
||||
//user.updateCosmetic(CosmeticSlot.MAINHAND);
|
||||
}
|
||||
|
||||
private void registerInventoryClickListener() {
|
||||
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(HMCCosmeticsPlugin.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Client.WINDOW_CLICK) {
|
||||
@Override
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBackpackType;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType;
|
||||
import com.hibiscusmc.hmccosmetics.entities.BalloonEntity;
|
||||
import com.hibiscusmc.hmccosmetics.nms.NMSHandlers;
|
||||
import com.hibiscusmc.hmccosmetics.util.InventoryUtils;
|
||||
import com.hibiscusmc.hmccosmetics.util.PlayerUtils;
|
||||
import com.hibiscusmc.hmccosmetics.util.packets.PacketManager;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -33,8 +34,14 @@ public class CosmeticUser {
|
||||
|
||||
// Cosmetic Settings/Toggles
|
||||
private boolean hideBackpack;
|
||||
private boolean hideCosmetics;
|
||||
private HashMap<CosmeticSlot, Color> colors = new HashMap<>();
|
||||
|
||||
public CosmeticUser() {
|
||||
hideBackpack = false;
|
||||
hideCosmetics = false;
|
||||
}
|
||||
|
||||
|
||||
public CosmeticUser(UUID uuid) {
|
||||
this.uniqueId = uuid;
|
||||
@@ -129,6 +136,9 @@ public class CosmeticUser {
|
||||
|
||||
public ItemStack getUserCosmeticItem(Cosmetic cosmetic) {
|
||||
ItemStack item = null;
|
||||
if (hideCosmetics) {
|
||||
return getPlayer().getInventory().getItem(InventoryUtils.getEquipmentSlot(cosmetic.getSlot()));
|
||||
}
|
||||
if (cosmetic instanceof CosmeticArmorType) {
|
||||
CosmeticArmorType cosmetic1 = (CosmeticArmorType) cosmetic;
|
||||
item = cosmetic1.getCosmeticItem();
|
||||
@@ -210,6 +220,11 @@ public class CosmeticUser {
|
||||
if (this.balloonEntity != null) return;
|
||||
|
||||
this.balloonEntity = NMSHandlers.getHandler().spawnBalloon(this, cosmeticBalloonType);
|
||||
|
||||
List<Player> viewer = PlayerUtils.getNearbyPlayers(player);
|
||||
viewer.add(player);
|
||||
|
||||
PacketManager.sendLeashPacket(getBalloonEntity().getPufferfishBalloonId(), player.getEntityId(), viewer);
|
||||
}
|
||||
|
||||
public void despawnBalloon() {
|
||||
@@ -296,4 +311,39 @@ public class CosmeticUser {
|
||||
hideBackpack = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void hideCosmetics() {
|
||||
if (hideCosmetics == true) return;
|
||||
hideCosmetics = true;
|
||||
if (hasCosmeticInSlot(CosmeticSlot.BALLOON)) {
|
||||
getBalloonEntity().removePlayerFromModel(getPlayer());
|
||||
List<Player> viewer = PlayerUtils.getNearbyPlayers(getPlayer());
|
||||
PacketManager.sendLeashPacket(getBalloonEntity().getPufferfishBalloonId(), -1, viewer);
|
||||
}
|
||||
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
|
||||
//CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK);
|
||||
getPlayer().removePassenger(invisibleArmorstand);
|
||||
invisibleArmorstand.getEquipment().clear();
|
||||
}
|
||||
updateCosmetic();
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("HideCosmetics");
|
||||
}
|
||||
|
||||
public void showCosmetics() {
|
||||
if (hideCosmetics == false) return;
|
||||
hideCosmetics = false;
|
||||
if (hasCosmeticInSlot(CosmeticSlot.BALLOON)) {
|
||||
CosmeticBalloonType balloonType = (CosmeticBalloonType) getCosmetic(CosmeticSlot.BALLOON);
|
||||
getBalloonEntity().addPlayerToModel(getPlayer(), balloonType.getModelName());
|
||||
List<Player> viewer = PlayerUtils.getNearbyPlayers(getPlayer());
|
||||
PacketManager.sendLeashPacket(getBalloonEntity().getPufferfishBalloonId(), getPlayer().getEntityId(), viewer);
|
||||
}
|
||||
if (hasCosmeticInSlot(CosmeticSlot.BACKPACK)) {
|
||||
CosmeticBackpackType cosmeticBackpackType = (CosmeticBackpackType) getCosmetic(CosmeticSlot.BACKPACK);
|
||||
ItemStack item = getUserCosmeticItem(cosmeticBackpackType);
|
||||
invisibleArmorstand.getEquipment().setHelmet(item);
|
||||
}
|
||||
updateCosmetic();
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("ShowCosmetics");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ public class InventoryUtils {
|
||||
|
||||
public static CosmeticSlot BukkitCosmeticSlot(EquipmentSlot slot) {
|
||||
return switch (slot) {
|
||||
case HAND -> CosmeticSlot.MAINHAND;
|
||||
case OFF_HAND -> CosmeticSlot.OFFHAND;
|
||||
case FEET -> CosmeticSlot.BOOTS;
|
||||
case LEGS -> CosmeticSlot.LEGGINGS;
|
||||
@@ -59,16 +60,16 @@ public class InventoryUtils {
|
||||
|
||||
public static CosmeticSlot BukkitCosmeticSlot(int slot) {
|
||||
switch (slot) {
|
||||
case 39 -> {
|
||||
case 36 -> {
|
||||
return CosmeticSlot.HELMET;
|
||||
}
|
||||
case 38 -> {
|
||||
case 37 -> {
|
||||
return CosmeticSlot.CHESTPLATE;
|
||||
}
|
||||
case 37 -> {
|
||||
case 38 -> {
|
||||
return CosmeticSlot.LEGGINGS;
|
||||
}
|
||||
case 36 -> {
|
||||
case 39 -> {
|
||||
return CosmeticSlot.BOOTS;
|
||||
}
|
||||
case 40 -> {
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -89,7 +90,6 @@ public class PacketManager extends BasePacket {
|
||||
if (cosmeticSlot == CosmeticSlot.BACKPACK || cosmeticSlot == CosmeticSlot.BALLOON) return;
|
||||
|
||||
NMSHandlers.getHandler().equipmentSlotUpdate(entityId, user, cosmeticSlot, sendTo);
|
||||
|
||||
}
|
||||
|
||||
public static void armorStandMetaPacket(
|
||||
|
||||
Reference in New Issue
Block a user