9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-29 20:09:13 +00:00

Fixed ProtocolLib enum null error on first call

This commit is contained in:
Fisher2911
2022-03-06 16:37:48 -05:00
parent 84864303fc
commit 1888145ee0
74 changed files with 76 additions and 26 deletions

View File

@@ -72,7 +72,7 @@ public class CosmeticsCommand extends CommandBase {
if (wardrobe.isActive() &&
!this.settings.getWardrobeSettings().inDistanceOfWardrobe(wardrobe.getCurrentLocation(), player.getLocation())) {
wardrobe.setActive(false);
wardrobe.despawnFakePlayer(player);
wardrobe.despawnFakePlayer(player, userManager);
this.messageHandler.sendMessage(
player,
Messages.CLOSED_WARDROBE

View File

@@ -32,17 +32,12 @@ public class WardrobeSettings {
private static final String ROTATION_SPEED_PATH = "rotation-speed";
private static final String SPAWN_DELAY_PATH = "spawn-delay";
private static final String DESPAWN_DELAY_PATH = "despawn-delay";
private static final String APPLY_COSMETICS_ON_CLOSE = "apply-cosmetcics-on-close";
private static final String OPEN_SOUND = "open-sound";
private static final String CLOSE_SOUND = "close-sound";
private static final String STATIC_LOCATION_PATH = "wardrobe-location";
private static final String VIEWER_LOCATION_PATH = "viewer-location";
private static final String LEAVE_LOCATION_PATH = "leave-location";
private static final String WORLD_PATH = "world";
private static final String X_PATH = "x";
private static final String Y_PATH = "y";
private static final String Z_PATH = "z";
private static final String YAW_PATH = "yaw";
private static final String PITCH_PATH = "pitch";
private final HMCCosmetics plugin;
@@ -54,6 +49,7 @@ public class WardrobeSettings {
private int rotationSpeed;
private int spawnDelay;
private int despawnDelay;
private boolean applyCosmeticsOnClose;
private SoundData openSound;
private SoundData closeSound;
private Location wardrobeLocation;
@@ -85,6 +81,7 @@ public class WardrobeSettings {
this.rotationSpeed = source.node(ROTATION_SPEED_PATH).getInt();
this.spawnDelay = source.node(SPAWN_DELAY_PATH).getInt();
this.despawnDelay = source.node(DESPAWN_DELAY_PATH).getInt();
this.applyCosmeticsOnClose = source.node(APPLY_COSMETICS_ON_CLOSE).getBoolean();
this.openSound = source.node(OPEN_SOUND).get(SoundData.class);
this.closeSound = source.node(CLOSE_SOUND).get(SoundData.class);
this.wardrobeLocation = source.node(STATIC_LOCATION_PATH).get(Location.class);
@@ -127,6 +124,10 @@ public class WardrobeSettings {
return despawnDelay;
}
public boolean isApplyCosmeticsOnClose() {
return applyCosmeticsOnClose;
}
public Location getWardrobeLocation() {
return this.wardrobeLocation.clone();
}

View File

@@ -20,7 +20,10 @@ import org.bukkit.entity.Entity;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
@@ -32,7 +35,7 @@ public class Database {
private final Dao<ArmorItemDAO, String> armorItemDao;
private final ConnectionSource dataSource;
private final DatabaseType databaseType;
AtomicInteger FAKE_ENTITY_ID = new AtomicInteger(Integer.MAX_VALUE);
private static final SplittableRandom RANDOM = new SplittableRandom();
public Database(
final HMCCosmetics plugin,
@@ -62,8 +65,8 @@ public class Database {
public void loadUser(final Entity entity, final Consumer<User> onComplete) {
final UUID uuid = entity.getUniqueId();
final int armorStandId = FAKE_ENTITY_ID.getAndDecrement();
final int balloonId = FAKE_ENTITY_ID.getAndDecrement();
final int armorStandId = getNextEntityId();
final int balloonId = getNextEntityId();
final Wardrobe wardrobe = this.createNewWardrobe(uuid);
Threads.getInstance().execute(
() -> {
@@ -103,8 +106,8 @@ public class Database {
}
public void loadNPCUser(final int id, final Entity entity, final Consumer<NPCUser> onComplete) {
final int armorStandId = FAKE_ENTITY_ID.getAndDecrement();
final int balloonId = FAKE_ENTITY_ID.getAndDecrement();
final int armorStandId = getNextEntityId();
final int balloonId = getNextEntityId();
Threads.getInstance().execute(
() -> {
try {
@@ -214,11 +217,15 @@ public class Database {
ownerUUID,
PlayerArmor.empty(),
new EntityIds(
FAKE_ENTITY_ID.getAndDecrement(),
FAKE_ENTITY_ID.getAndDecrement(),
FAKE_ENTITY_ID.getAndDecrement()
getNextEntityId(),
getNextEntityId(),
getNextEntityId()
),
false
);
}
public static int getNextEntityId() {
return RANDOM.nextInt(50_000, 100_000);
}
}

View File

@@ -93,8 +93,8 @@ public class DatabaseConverter {
this.database.createNewWardrobe(uuid),
new EntityIds(
-1,
this.database.FAKE_ENTITY_ID.getAndDecrement(),
this.database.FAKE_ENTITY_ID.getAndDecrement()
Database.getNextEntityId(),
Database.getNextEntityId()
)
);
final String backpackId = results.getString(2);

View File

@@ -36,8 +36,7 @@ public class ArmorItemDAO {
}
public static ArmorItemDAO fromArmorItem(final ArmorItem armorItem) {
return new ArmorItemDAO(armorItem.getId(), armorItem.getType().toString(),
armorItem.getDye());
return new ArmorItemDAO(armorItem.getId(), armorItem.getType().toString(), armorItem.getDye());
}
@Nullable

View File

@@ -59,7 +59,7 @@ public class JoinListener implements Listener {
if (wardrobe.isActive()) {
Bukkit.getScheduler().runTaskAsynchronously(
this.plugin,
() -> wardrobe.despawnFakePlayer(player)
() -> wardrobe.despawnFakePlayer(player, userManager)
);
}
});

View File

@@ -36,7 +36,7 @@ public class PlayerShiftListener implements Listener {
if (!wardrobe.isActive()) return;
wardrobe.despawnFakePlayer(player);
wardrobe.despawnFakePlayer(player, userManager);
this.plugin.getSettings().getWardrobeSettings().playCloseSound(player);
this.plugin.getMessageHandler().sendMessage(
player,

View File

@@ -95,7 +95,7 @@ public class Messages {
public static final Message TRADED_TOKEN =
new Message("traded-token", "<green>You have received the cosmetic: " + Placeholder.ID);
public static final Message ALREADY_UNLOCKED =
new Message("alread-unlocked", "<green>You have already unlocked the cosmetic: " + Placeholder.ID);
new Message("already-unlocked", "<green>You have already unlocked the cosmetic: " + Placeholder.ID);
public static final Message SET_OTHER_BACKPACK = new Message(
"set-other-backpack", "<green>You have set the backpack of " +
Placeholder.PLAYER + " to " + Placeholder.TYPE + "."

View File

@@ -124,7 +124,11 @@ public class PacketManager {
final PacketContainer armorPacket = new PacketContainer(
PacketType.Play.Server.ENTITY_EQUIPMENT);
armorPacket.getIntegers().write(0, entityId);
armorPacket.getSlotStackPairLists().write(0, equipmentList);
try {
armorPacket.getSlotStackPairLists().write(0, equipmentList);
// for some reason ProtocolLib throws an error the first time this is called
} catch (final NullPointerException ignored) {}
return armorPacket;
}

View File

@@ -6,7 +6,11 @@ import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Nullable;
@@ -39,6 +43,13 @@ public class User extends BaseUser<UUID> {
final Player player = this.getPlayer();
if (player == null) return false;
if (player.getUniqueId().equals(other.getUniqueId()) && this.hidden) return false;
final ItemStack itemStack = player.getInventory().getItemInMainHand();
if (itemStack != null && itemStack.getType() == Material.TRIDENT) {
final ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta != null && itemMeta.hasEnchant(Enchantment.RIPTIDE)) {
return false;
}
}
return player.getGameMode() != GameMode.SPECTATOR &&
(!player.hasPotionEffect(PotionEffectType.INVISIBILITY) &&
other.canSee(player) &&

View File

@@ -14,6 +14,9 @@ import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
@@ -116,7 +119,7 @@ public class Wardrobe extends User {
this.startSpinTask(viewer);
}
public void despawnFakePlayer(final Player viewer) {
public void despawnFakePlayer(final Player viewer, final UserManager userManager) {
this.active = false;
final WardrobeSettings settings = this.plugin.getSettings().getWardrobeSettings();
Bukkit.getScheduler().runTaskLaterAsynchronously(
@@ -135,6 +138,19 @@ public class Wardrobe extends User {
this.spawned = false;
this.cameraLocked = false;
this.currentLocation = null;
final Collection<ArmorItem> armorItems = new ArrayList<>(this.getPlayerArmor().getArmorItems());
if (settings.isApplyCosmeticsOnClose()) {
final Optional<User> optionalUser = userManager.get(this.ownerUUID);
optionalUser.ifPresent(user -> Bukkit.getScheduler().runTask(
plugin,
() -> {
for (final ArmorItem armorItem : armorItems) {
if (!user.hasPermissionToUse(armorItem)) continue;
userManager.setItem(user, armorItem);
}
}
));
}
this.getPlayerArmor().clear();
Bukkit.getScheduler().runTask(this.plugin, () -> {
if (viewer == null || !viewer.isOnline()) return;

View File

@@ -35,6 +35,8 @@ wardrobe:
spawn-delay: 40
# how long in ticks until the wardrobe should be despawned
despawn-delay: 40
# if cosmetics that the user have permissions for should be applied on close of wardrobe
apply-cosmetcics-on-close: true
open-sound:
sound: # Play a sound
name: "minecraft:block.chain.break"

View File

@@ -38,6 +38,10 @@ wardrobe-already-open: "%prefix% <gradient:#6D9DC5:#45CDE9><red>The wardrobe is
not-near-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9><red>You are not near the wardrobe!"
cannot-use-portable-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9><red>You cannot use the portable wardrobe!"
opened-other-wardrobe: "%prefix% <gradient:#6D9DC5:#45CDE9><red>Opening %player%'s wardrobe."
gave-token: "%prefix% <gradient:#6D9DC5:#45CDE9><green>You gave %player% a %id% token"
received-token: "%prefix% <gradient:#6D9DC5:#45CDE9><green>You were given a %id% token"
traded-token: "%prefix% <gradient:#6D9DC5:#45CDE9><green>You have received the cosmetic: %id%"
already-unlocked: "%prefix% <gradient:#6D9DC5:#45CDE9><green>You have already unlocked the cosmetic: %id%"
help-command: "<#6D9DC5><st> </st> %prefix% <gradient:#40B7D6:#6D9DC5>HMCCosmetics - Help</gradient> %prefix% <#6D9DC5> <st> </st>