Skip to content

Commit

Permalink
Merge pull request #411 from lokka30/3.7-dev
Browse files Browse the repository at this point in the history
3.7 dev
  • Loading branch information
lokka30 authored Sep 14, 2022
2 parents 2501a53 + 2547f60 commit 8b13ee5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.lokka30</groupId>
<artifactId>LevelledMobs</artifactId>
<version>3.7.0 b691</version>
<version>3.7.0 b693</version>
<packaging>jar</packaging>
<name>LevelledMobs</name>
<description>The Ultimate RPG Mob Levelling Plugin</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package me.lokka30.levelledmobs.commands.subcommands;

import me.lokka30.levelledmobs.misc.LivingEntityPlaceHolder;
import me.lokka30.levelledmobs.misc.LivingEntityPlaceholder;
import me.lokka30.levelledmobs.misc.LivingEntityPlaceholder;
import me.lokka30.levelledmobs.misc.RequestedLevel;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class SummonMobOptions {

SummonMobOptions(@NotNull final LivingEntityPlaceHolder lmPlaceHolder,
final CommandSender sender) {
this.lmPlaceHolder = lmPlaceHolder;
SummonMobOptions(
@NotNull final LivingEntityPlaceholder lmPlaceholder,
@NotNull final CommandSender sender
) {
this.lmPlaceholder = lmPlaceholder;
this.sender = sender;
}

@NotNull final LivingEntityPlaceHolder lmPlaceHolder;
public final LivingEntityPlaceholder lmPlaceholder;
public final CommandSender sender;
SummonSubcommand.SummonType summonType;
public SummonSubcommand.SummonType summonType;
public int amount;
RequestedLevel requestedLevel;
public RequestedLevel requestedLevel;
public Player player;
public boolean override;
String nbtData;
public String nbtData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import me.lokka30.levelledmobs.managers.LevelManager;
import me.lokka30.levelledmobs.misc.AdditionalLevelInformation;
import me.lokka30.levelledmobs.misc.LevellableState;
import me.lokka30.levelledmobs.misc.LivingEntityPlaceHolder;
import me.lokka30.levelledmobs.misc.LivingEntityPlaceholder;
import me.lokka30.levelledmobs.misc.LivingEntityWrapper;
import me.lokka30.levelledmobs.misc.RequestedLevel;
import me.lokka30.levelledmobs.util.PaperUtils;
Expand All @@ -39,6 +39,7 @@
* Summons a levelled mob with a specific level and criteria
*
* @author stumper66
* @author lokka30
* @since v2.0.0
*/
public class SummonSubcommand extends MessagesBase implements Subcommand {
Expand All @@ -48,8 +49,12 @@ public SummonSubcommand(final LevelledMobs main) {
}

@Override
public void parseSubcommand(final LevelledMobs main, final @NotNull CommandSender sender,
final String label, @NotNull final String @NotNull [] args) {
public void parseSubcommand(
@NotNull final LevelledMobs main,
@NotNull final CommandSender sender,
@NotNull final String label,
@NotNull final String... args
) {
commandSender = sender;
messageLabel = label;

Expand Down Expand Up @@ -169,7 +174,7 @@ private void parseSubcommand2(final String @NotNull [] args, final boolean overr
return;
}

final LivingEntityPlaceHolder lmPlaceHolder = LivingEntityPlaceHolder.getInstance(
final LivingEntityPlaceholder lmPlaceHolder = LivingEntityPlaceholder.getInstance(
entityType, location, main);

final SummonMobOptions options = new SummonMobOptions(lmPlaceHolder, commandSender);
Expand Down Expand Up @@ -213,7 +218,7 @@ private void parseSubcommand2(final String @NotNull [] args, final boolean overr
return;
}

final LivingEntityPlaceHolder lmPlaceHolder = LivingEntityPlaceHolder.getInstance(
final LivingEntityPlaceholder lmPlaceHolder = LivingEntityPlaceholder.getInstance(
entityType, location, main);
final SummonMobOptions options = new SummonMobOptions(lmPlaceHolder, commandSender);
options.amount = amount;
Expand Down Expand Up @@ -261,7 +266,7 @@ private void parseSubcommand2(final String @NotNull [] args, final boolean overr
if (location == null || location.getWorld() == null) {
showMessage("command.levelledmobs.summon.atLocation.invalid-location");
} else {
final LivingEntityPlaceHolder lmPlaceHolder = LivingEntityPlaceHolder.getInstance(
final LivingEntityPlaceholder lmPlaceHolder = LivingEntityPlaceholder.getInstance(
entityType, location, main);
final SummonMobOptions options = new SummonMobOptions(lmPlaceHolder,
commandSender);
Expand Down Expand Up @@ -395,30 +400,30 @@ enum SummonType {
private void summonMobs(@NotNull final SummonMobOptions options) {

final CommandSender sender = options.sender;
final LevelledMobs main = options.lmPlaceHolder.getMainInstance();
final LevelledMobs main = options.lmPlaceholder.getMainInstance();
final Player target = options.player;
Location location = options.lmPlaceHolder.getLocation();
Location location = options.lmPlaceholder.getLocation();

if (main.levelManager.FORCED_BLOCKED_ENTITY_TYPES.contains(
options.lmPlaceHolder.getEntityType())) {
options.lmPlaceholder.getEntityType())) {
List<String> messages = main.messagesCfg.getStringList(
"command.levelledmobs.summon.not-levellable");
messages = Utils.replaceAllInList(messages, "%prefix%", main.configUtils.getPrefix());
messages = Utils.replaceAllInList(messages, "%entity%",
options.lmPlaceHolder.getTypeName());
options.lmPlaceholder.getTypeName());
messages = Utils.colorizeAllInList(messages);
messages.forEach(sender::sendMessage);
return;
}

if (!sender.isOp() && !options.override
&& main.levelInterface.getLevellableState(options.lmPlaceHolder)
&& main.levelInterface.getLevellableState(options.lmPlaceholder)
!= LevellableState.ALLOWED) {
List<String> messages = main.messagesCfg.getStringList(
"command.levelledmobs.summon.not-levellable");
messages = Utils.replaceAllInList(messages, "%prefix%", main.configUtils.getPrefix());
messages = Utils.replaceAllInList(messages, "%entity%",
options.lmPlaceHolder.getTypeName());
options.lmPlaceholder.getTypeName());
messages = Utils.colorizeAllInList(messages);
messages.forEach(sender::sendMessage);
return;
Expand All @@ -445,7 +450,7 @@ private void summonMobs(@NotNull final SummonMobOptions options) {
messages.forEach(sender::sendMessage);
}

final int[] levels = main.levelManager.getMinAndMaxLevels(options.lmPlaceHolder);
final int[] levels = main.levelManager.getMinAndMaxLevels(options.lmPlaceholder);
final int minLevel = levels[0];
final int maxLevel = levels[1];

Expand Down Expand Up @@ -515,11 +520,12 @@ private void summonMobs(@NotNull final SummonMobOptions options) {
options.requestedLevel.level;

final Entity entity = location.getWorld()
.spawnEntity(location, options.lmPlaceHolder.getEntityType());
.spawnEntity(location, options.lmPlaceholder.getEntityType());

if (entity instanceof LivingEntity) {
final LivingEntityWrapper lmEntity = LivingEntityWrapper.getInstance(
(LivingEntity) entity, main);
final LivingEntityWrapper lmEntity = LivingEntityWrapper
.getInstance((LivingEntity) entity, main);

lmEntity.setSummonedLevel(useLevel);
lmEntity.isNewlySpawned = true;
synchronized (LevelManager.summonedOrSpawnEggs_Lock) {
Expand All @@ -542,20 +548,21 @@ private void summonMobs(@NotNull final SummonMobOptions options) {
switch (options.summonType) {
case HERE -> showMessage("command.levelledmobs.summon.here.success",
new String[]{"%amount%", "%level%", "%entity%"},
new String[]{String.valueOf(options.amount), options.requestedLevel.toString(),
options.lmPlaceholder.getTypeName()}
);
case AT_LOCATION -> showMessage("command.levelledmobs.summon.atLocation.success",
new String[]{"%amount%", "%level%", "%entity%", "%x%", "%y%", "%z%", "%world%"},
new String[]{
String.valueOf(options.amount),
options.requestedLevel.toString(),
options.lmPlaceHolder.getTypeName()
options.lmPlaceholder.getTypeName(),
Integer.toString(location.getBlockX()),
Integer.toString(location.getBlockY()),
Integer.toString(location.getBlockZ()),
location.getWorld() == null ? "(null)" : location.getWorld().getName()
}
);
case AT_LOCATION -> showMessage("command.levelledmobs.summon.atLocation.success",
new String[]{"%amount%", "%level%", "%entity%", "%x%", "%x%", "%x%", "%world%"},
new String[]{String.valueOf(options.amount), options.requestedLevel.toString(),
options.lmPlaceHolder.getTypeName(),
String.valueOf(location.getBlockX()), String.valueOf(location.getBlockY()),
String.valueOf(location.getBlockX()),
location.getWorld() == null ? "(null)" : location.getWorld().getName()}
);
case AT_PLAYER -> {

final String playerName = VersionUtils.isRunningPaper() ?
Expand All @@ -565,7 +572,7 @@ private void summonMobs(@NotNull final SummonMobOptions options) {
new String[]{"%amount%", "%level%", "%entity%", "%targetUsername%",
"%targetDisplayname%"},
new String[]{String.valueOf(options.amount), options.requestedLevel.toString(),
options.lmPlaceHolder.getTypeName(),
options.lmPlaceholder.getTypeName(),
target == null ? "(null)" : target.getName(),
target == null ? "(null)" : playerName}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@
* @author stumper66
* @since 3.0.0
*/
public class LivingEntityPlaceHolder extends LivingEntityWrapperBase implements
LivingEntityInterface {
public class LivingEntityPlaceholder
extends LivingEntityWrapperBase
implements
LivingEntityInterface
{

private LivingEntityPlaceHolder(final @NotNull LevelledMobs main) {
private LivingEntityPlaceholder(final @NotNull LevelledMobs main) {
super(main);
}

private EntityType entityType;
private final static Object cachedPlaceHolders_Lock = new Object();
private final static Stack<LivingEntityPlaceHolder> cache = new Stack<>();
private final static Stack<LivingEntityPlaceholder> cache = new Stack<>();

@NotNull public static LivingEntityPlaceHolder getInstance(final EntityType entityType,
@NotNull
public static LivingEntityPlaceholder getInstance(final EntityType entityType,
final @NotNull Location location, final @NotNull LevelledMobs main) {
final LivingEntityPlaceHolder leph;
final LivingEntityPlaceholder leph;

if (location.getWorld() == null) {
throw new NullPointerException("World can't be null");
}

synchronized (cachedPlaceHolders_Lock) {
if (cache.empty()) {
leph = new LivingEntityPlaceHolder(main);
leph = new LivingEntityPlaceholder(main);
} else {
leph = cache.pop();
}
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/me/lokka30/levelledmobs/nms/NMSHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import me.lokka30.levelledmobs.LevelledMobs;
import me.lokka30.levelledmobs.managers.ExternalCompatibilityManager;
import me.lokka30.levelledmobs.util.Utils;
import me.lokka30.microlib.messaging.MessageUtils;
import me.lokka30.microlib.other.VersionUtils;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -19,13 +21,7 @@ public class NMSHandler {

public NMSHandler(final @NotNull LevelledMobs main) {
this.main = main;
boolean hasKiori = false;
try {
Class.forName("net.kyori.adventure.text.Component");
hasKiori = true;
} catch (ClassNotFoundException ignored) { }

this.hasKiori = hasKiori;
this.hasPaper = VersionUtils.isRunningPaper();
parseBukkitVersion();
}

Expand All @@ -36,7 +32,7 @@ public NMSHandler(final @NotNull LevelledMobs main) {
private NMSUtil currentUtil;
public double minecraftVersion;
public boolean isUsingProtocolLib;
public final boolean hasKiori;
public final boolean hasPaper;

private void parseBukkitVersion() {
// example: org.bukkit.craftbukkit.v1_18_R2.CraftServer
Expand Down Expand Up @@ -75,10 +71,10 @@ private void parseBukkitVersion() {
// supported is paper >= 1.18 or spigot >= 1.19
// otherwise protocollib is used

if (hasKiori && this.minecraftVersion >= 1.18 ||
!hasKiori && this.minecraftVersion >= 1.19) {
if (hasPaper && this.minecraftVersion >= 1.18 ||
!hasPaper && this.minecraftVersion >= 1.19) {
// 1.18 and newer we support with direct nms
this.currentUtil = new NametagSender(nmsVersionString, hasKiori);
this.currentUtil = new NametagSender(nmsVersionString, hasPaper);
Utils.logger.info(
String.format("Using NMS version %s for nametag support", nmsVersionString));
} else if (ExternalCompatibilityManager.hasProtocolLibInstalled()) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/me/lokka30/levelledmobs/nms/NametagSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -79,7 +78,6 @@ public void sendNametag(final @NotNull LivingEntity livingEntity, @NotNull Namet
}
}

@SuppressWarnings("deprecation")
private @NotNull Optional<Component> buildNametagComponent(final @NotNull LivingEntity livingEntity,
final @NotNull NametagResult nametag){
if (nametag.isNullOrEmpty())
Expand Down Expand Up @@ -118,7 +116,7 @@ public void sendNametag(final @NotNull LivingEntity livingEntity, @NotNull Namet
null;

final Component mobNameComponent = nametag.overriddenName == null ?
Component.translatable(Bukkit.getUnsafe().getTranslationKey(livingEntity.getType())) :
Component.translatable(getTranslationKey(livingEntity.getType())) :
Component.literal(nametag.overriddenName);

MutableComponent comp = Component.empty();
Expand All @@ -138,6 +136,11 @@ public void sendNametag(final @NotNull LivingEntity livingEntity, @NotNull Namet
return Optional.of(comp);
}

@SuppressWarnings("deprecation")
private String getTranslationKey(final org.bukkit.entity.@NotNull EntityType type) {
return net.minecraft.world.entity.EntityType.byString(type.getName()).map(net.minecraft.world.entity.EntityType::getDescriptionId).orElse(null);
}

private void buildReflection(){
try {
this.clazz_CraftChatMessage = Class.forName(
Expand Down

0 comments on commit 8b13ee5

Please sign in to comment.