Skip to content

Commit

Permalink
Merge pull request #313 from lokka30/3.2-dev
Browse files Browse the repository at this point in the history
3.2 dev
  • Loading branch information
lokka30 authored Oct 22, 2021
2 parents d79ea84 + 0cedbb1 commit e447614
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 158 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>me.lokka30</groupId>
<artifactId>LevelledMobs</artifactId>
<version>3.2.2 b538</version>
<version>3.2.3 b546</version>
<packaging>jar</packaging>

<name>LevelledMobs</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/lokka30/levelledmobs/Companion.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void registerListeners() {
main.levelManager = new LevelManager(main);
main._mobsQueueManager.start();
main.nametagQueueManager_.start();
main.levelManager.entitySpawnListener = new EntitySpawnListener(main); // we're saving this reference so the summon command has access to it
main.levelManager.entitySpawnListener = new EntitySpawnListener(main);
main.levelManager.entitySpawnListener.processMobSpawns = main.helperSettings.getBoolean(main.settingsCfg, "level-mobs-upon-spawn", true);
main.entityDamageDebugListener = new EntityDamageDebugListener(main);
main.blockPlaceListener = new BlockPlaceListener(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import me.lokka30.levelledmobs.LevelledMobs;
import me.lokka30.levelledmobs.misc.Utils;
import me.lokka30.levelledmobs.rules.DoNotMerge;
import me.lokka30.microlib.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -35,8 +37,9 @@ public class SpawnerSubCommand implements Subcommand{

final private LevelledMobs main;
final private List<String> allSpawnerOptions = Arrays.asList(
"/name", "/customdropid", "/spawntype", "/minlevel", "/maxlevel", "/delay", "/maxnearbyentities",
"/minspawndelay", "/maxspawndelay", "/requiredplayerrange", "/spawncount", "/spawnrange"
"/name", "/customdropid", "/spawntype", "/giveplayer", "/lore", "/minlevel", "/maxlevel", "/delay",
"/maxnearbyentities", "/minspawndelay", "/maxspawndelay", "/requiredplayerrange",
"/spawncount", "/spawnrange", "/nolore"
);
private boolean hadInvalidArg;

Expand All @@ -47,11 +50,6 @@ public void parseSubcommand(final LevelledMobs main, @NotNull final CommandSende
return;
}

if (!(sender instanceof Player)){
sender.sendMessage("Command can only be run by a player");
return;
}

if (args.length < 2){
List<String> messages = main.messagesCfg.getStringList("command.levelledmobs.spawner.usage");
messages = Utils.replaceAllInList(messages, "%prefix%", main.configUtils.getPrefix());
Expand All @@ -61,6 +59,33 @@ public void parseSubcommand(final LevelledMobs main, @NotNull final CommandSende
return;
}

OperationEnum operationEnum = OperationEnum.CREATE;

switch (args[1].toLowerCase()){
case "copy":
operationEnum = OperationEnum.COPY;
break;
case "info":
operationEnum = OperationEnum.INFO;
break;
}

boolean hasGivePlayer = false;
for (int i = 2; i < args.length; i++){
if ("/giveplayer".equalsIgnoreCase(args[i])){
hasGivePlayer = true;
break;
}
}

if ((!hasGivePlayer || operationEnum != OperationEnum.CREATE) && !(sender instanceof Player)){
if (operationEnum == OperationEnum.CREATE)
sender.sendMessage("Command can only be run by a player unless /giveplayer is specified");
else
sender.sendMessage("Command can only be run by a player");
return;
}

switch (args[1].toLowerCase()){
case "create":
parseCreateCommand(sender, label, args);
Expand Down Expand Up @@ -186,10 +211,21 @@ else if (!needsListener && main.companion.playerInteractListenerIsRegistered){
private void parseCreateCommand(@NotNull final CommandSender sender, final String label, final String[] args){
hadInvalidArg = false;

final CustomSpawnerInfo info = new CustomSpawnerInfo(main, (Player) sender, label);
final CustomSpawnerInfo info = new CustomSpawnerInfo(main, label);
if (sender instanceof Player)
info.player = (Player) sender;

// arguments with no values go here:
for (int i = 1; i < args.length; i++) {
final String arg = args[i];
if ("/nolore".equalsIgnoreCase(arg)) {
info.noLore = true;
break;
}
}

for (int i = 0; i < allSpawnerOptions.size(); i++){
final boolean mustBeANumber = (i > 2);
for (int i = 0; i < allSpawnerOptions.size() - 1; i++){
final boolean mustBeANumber = (i > 4);
final String command = allSpawnerOptions.get(i);
final String foundValue = getArgValue(command, args, sender, label, mustBeANumber);
if (hadInvalidArg) return;
Expand All @@ -198,6 +234,7 @@ private void parseCreateCommand(@NotNull final CommandSender sender, final Strin
switch (command){
case "/name": info.customName = foundValue; break;
case "/customdropid": info.customDropId = foundValue; break;
case "/lore": info.customLore = foundValue; break;
case "/spawntype":
try{
info.spawnType = EntityType.valueOf(foundValue.toUpperCase());
Expand All @@ -216,6 +253,21 @@ private void parseCreateCommand(@NotNull final CommandSender sender, final Strin
case "/requiredplayerrange": info.requiredPlayerRange = Integer.parseInt(foundValue); break;
case "/spawncount": info.spawnCount = Integer.parseInt(foundValue); break;
case "/spawnrange": info.spawnRange = Integer.parseInt(foundValue); break;
case "/giveplayer":
if (Utils.isNullOrEmpty(foundValue)){
sender.sendMessage("No player was specified");
return;
}
try { info.player = Bukkit.getPlayer(foundValue); }
catch (Exception e){
sender.sendMessage("Invalid or offline player: " + foundValue);
return;
}
if (info.player == null){
sender.sendMessage("Invalid or offline player: " + foundValue);
return;
}
break;
}
}

Expand All @@ -228,6 +280,11 @@ private void parseCreateCommand(@NotNull final CommandSender sender, final Strin
return;
}

if (info.player == null){
sender.sendMessage("No player was specified");
return;
}

generateSpawner(info);
}

Expand All @@ -243,8 +300,10 @@ private String getArgValue(final String key, final String @NotNull [] args, fina
keyFlag = i;
else if (keyFlag == i - 1 && arg.startsWith("\""))
nameStartFlag = i;
else if (nameStartFlag > -1 && !arg.startsWith("/") && arg.endsWith("\""))
else if (nameStartFlag > -1 && !arg.startsWith("/") && arg.endsWith("\"")) {
nameEndFlag = i;
break;
}
}

if (keyFlag < 0) return null;
Expand Down Expand Up @@ -302,6 +361,8 @@ public static void generateSpawner(final @NotNull CustomSpawnerInfo info){
info.maxSpawnDelay = info.minSpawnDelay;
}

if (info.customName != null) info.customName = MessageUtils.colorizeAll(info.customName);

final ItemStack item = new ItemStack(Material.SPAWNER);
final ItemMeta meta = item.getItemMeta();
if (meta != null){
Expand All @@ -315,8 +376,7 @@ public static void generateSpawner(final @NotNull CustomSpawnerInfo info){
if (!Modifier.isPublic(f.getModifiers())) continue;
if (f.get(info) == null) continue;
final String name = f.getName();
if (name.equals("player") || name.equals("label") || name.equals("customName") || name.equals("main"))
continue;
if (f.isAnnotationPresent(DoNotMerge.class)) continue;

if ("-1".equals(f.get(info).toString()) && (name.equals("minLevel") || name.equals("maxLevel")))
continue;
Expand All @@ -338,7 +398,7 @@ public static void generateSpawner(final @NotNull CustomSpawnerInfo info){
e.printStackTrace();
}

if (info.lore == null) {
if (!info.noLore && info.lore == null && info.customLore == null) {
lore = Utils.colorizeAllInList(lore);
meta.setLore(lore);

Expand All @@ -349,11 +409,14 @@ public static void generateSpawner(final @NotNull CustomSpawnerInfo info){
}
meta.getPersistentDataContainer().set(info.main.namespaced_keys.keySpawner_Lore, PersistentDataType.STRING, sbLore.toString());
}
else {
else if (!info.noLore || info.customLore != null){
final String useLore = info.customLore == null ?
info.lore : MessageUtils.colorizeAll(info.customLore).replace("\\n", "\n");

lore.clear();
lore.addAll(Arrays.asList(info.lore.split("\n")));
lore.addAll(List.of(useLore.split("\n")));
meta.setLore(lore);
meta.getPersistentDataContainer().set(info.main.namespaced_keys.keySpawner_Lore, PersistentDataType.STRING, info.lore);
meta.getPersistentDataContainer().set(info.main.namespaced_keys.keySpawner_Lore, PersistentDataType.STRING, useLore);
}

meta.getPersistentDataContainer().set(info.main.namespaced_keys.keySpawner, PersistentDataType.INTEGER, 1);
Expand Down Expand Up @@ -460,6 +523,12 @@ else if (("info".equalsIgnoreCase(args[1]) || "copy".equalsIgnoreCase(args[1]))
case "/spawncount":
case "/spawnrange":
return Collections.singletonList("4");
case "/giveplayer":
final List<String> players = new LinkedList<>();
for (final Player player : Bukkit.getOnlinePlayers())
players.add(player.getName());
players.sort(String.CASE_INSENSITIVE_ORDER);
return players;
}
}

Expand All @@ -482,28 +551,32 @@ else if (inQuotes && arg.endsWith("\""))
final String lastArg = args[args.length - 1];

if (inQuotes || lastArg.length() > 0 && lastArg.charAt(lastArg.length() - 1) == '\"')
return Collections.singletonList("");
return Collections.emptyList();

final List<String> result = new ArrayList<>(commandsList.size());
result.addAll(commandsList);
return result;
}

public static class CustomSpawnerInfo{
public CustomSpawnerInfo(final LevelledMobs main, final Player player, final String label){
public CustomSpawnerInfo(final LevelledMobs main, final String label){
this.main = main;
this.player = player;
this.label = label;
this.minLevel = -1;
this.maxLevel = -1;
this.spawnType = EntityType.UNKNOWN;
}

@DoNotMerge
final public LevelledMobs main;
final public Player player;
@DoNotMerge
final public String label;
@DoNotMerge
public Player player;
public int minLevel;
public int maxLevel;
@DoNotMerge
public boolean noLore;
public Integer delay;
public Integer maxNearbyEntities;
public Integer minSpawnDelay;
Expand All @@ -512,8 +585,17 @@ public CustomSpawnerInfo(final LevelledMobs main, final Player player, final Str
public Integer spawnCount;
public Integer spawnRange;
public String customDropId;
@DoNotMerge
public String customName;
public EntityType spawnType;
@DoNotMerge
public String customLore;
public String lore;
}

private enum OperationEnum{
CREATE,
COPY,
INFO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.lokka30.levelledmobs.commands.subcommands;

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 {
public SummonMobOptions(@NotNull final LivingEntityPlaceHolder lmPlaceHolder, final CommandSender sender){
this.lmPlaceHolder = lmPlaceHolder;
this.sender = sender;
}

@NotNull
public final LivingEntityPlaceHolder lmPlaceHolder;
public final CommandSender sender;
public SummonSubcommand.SummonType summonType;
public int amount;
public RequestedLevel requestedLevel;
public Player player;
public boolean override;
public String nbtData;
}
Loading

0 comments on commit e447614

Please sign in to comment.