Skip to content

Commit

Permalink
Merge pull request #52 from PikaMug/master
Browse files Browse the repository at this point in the history
Java 8 compatibility
  • Loading branch information
Pyrbu authored May 14, 2023
2 parents 21638dd + a977a7c commit 271b087
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gonalez/ZNetwork. This fork was made because the original maintainer of the plug
in the original project's [official discord server](https://discord.com/invite/RhNMH4T).

### Dependencies
- Java 17
- Java 8
- Spigot 1.8 - 1.19.4
- PlaceholderAPI (OPTIONAL)

Expand Down
12 changes: 5 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ dependencies {
compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT"
compileOnly "me.clip:placeholderapi:2.11.1"

compileOnly "com.mojang:authlib:3.4.40"
compileOnly "com.mojang:datafixerupper:6.0.8"
compileOnly "com.mojang:authlib:2.1.28"
compileOnly "com.mojang:datafixerupper:3.0.25"

implementation "commons-io:commons-io:2.11.0"
implementation "com.google.code.gson:gson:2.10.1"
Expand All @@ -41,10 +41,8 @@ dependencies {

group "lol.pyr"
version "1.0.6"

compileJava {
options.release.set(17)
}
sourceCompatibility 1.8
targetCompatibility 1.8

shadowJar {
archiveClassifier.set ""
Expand Down Expand Up @@ -93,4 +91,4 @@ publishing {
url = uri("https://repo.pyr.lol/releases/")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Set<CommandTabInformation> getCommandsTab() {

public boolean execute(CommandSender sender, String commandLabel, String[] args) {
Optional<Map.Entry<CommandInformation, CommandInvoker>> subCommandOptional = this.subCommands.entrySet().stream().filter(command -> command.getKey().name().contentEquals((args.length > 0) ? args[0] : "")).findFirst();
if (subCommandOptional.isEmpty()) {
if (!subCommandOptional.isPresent()) {
sender.sendMessage(ChatColor.RED + "Unable to locate the following command: " + commandLabel + ".");
return false;
}
Expand All @@ -90,7 +90,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)

public @NotNull List<String> tabComplete(CommandSender sender, String commandLabel, String[] args) {
Optional<Map.Entry<CommandTabInformation, CommandTabInvoker>> subCommandOptional = this.subCommandsTab.entrySet().stream().filter(command -> command.getKey().name().contentEquals((args.length > 0) ? args[0] : "")).findFirst();
if (subCommandOptional.isEmpty()) {
if (!subCommandOptional.isPresent()) {
List<String> collect = getCommands().stream().map(CommandInformation::name).collect(Collectors.toList());
if (args.length == 0) {
return collect;
Expand All @@ -104,7 +104,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
}
return subCommand.getValue().tabComplete(new io.github.znetworkw.znpcservers.commands.CommandSender(sender), Arrays.copyOfRange(args, 1, args.length));
} catch (CommandException e) {
return List.of();
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public CommandInvoker(Command command, Method commandMethod, String permission)

public void execute(CommandSender sender, Object command) throws CommandException {
// Check if the sender is not a player
if (!(sender.getCommandSender() instanceof Player player))
if (!(sender.getCommandSender() instanceof Player))
throw new CommandException("Only players may execute this command.");

final Player player = (Player)sender.getCommandSender();

// Check if the permission is not empty and the player does not have the permission
if (this.permission.length() > 0 && !player.hasPermission(this.permission))
throw new CommandPermissionException("You cannot execute this command.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Player;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

public class CommandTabInvoker {
Expand All @@ -21,21 +22,24 @@ public CommandTabInvoker(Command command, Method commandMethod, String permissio

public List<String> tabComplete(CommandSender sender, Object command) throws CommandException {
// Check if the sender is not a player
if (!(sender.getCommandSender() instanceof Player player))
if (!(sender.getCommandSender() instanceof Player))
throw new CommandException("Only players may execute this command.");

final Player player = (Player)sender.getCommandSender();

// Check if the permission is not empty and the player does not have the permission
if (this.permission.length() > 0 && !player.hasPermission(this.permission))
throw new CommandPermissionException("You cannot execute this command.");

try {
// Command Tab execution
Object invoke = this.commandTabMethod.invoke(this.command, sender, command);
if (invoke instanceof List<?> list) return ((List<String>) list);
if (invoke instanceof List<?>)
return ((List<String>) invoke);
} catch (IllegalAccessException | ClassCastException | java.lang.reflect.InvocationTargetException e) {
throw new CommandExecuteException(e.getMessage(), e.getCause());
}

return List.of();
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,9 @@ public List<String> actionTabComplete(CommandSender sender, String[] args) {
}

switch (args[0].toLowerCase()) {
default -> {
default:
return Lists.newArrayList();
}
case "add" -> {
case "add":
if (args.length == 2) {
return Lists.newArrayList("<npc_id>");
}
Expand All @@ -443,36 +442,28 @@ public List<String> actionTabComplete(CommandSender sender, String[] args) {
if (args.length == 4) {
NPCAction.ActionType actionType = NPCAction.ActionType.valueOf(args[2].toUpperCase());
switch (actionType) {
default -> {
default:
return Lists.newArrayList();
}
case MESSAGE -> {
case MESSAGE:
return Lists.newArrayList("<message>");
}
case CMD -> {
case CMD:
return Lists.newArrayList("<command>");
}
case CONSOLE -> {
case CONSOLE:
return Lists.newArrayList("<console command>");
}
case CHAT -> {
case CHAT:
return Lists.newArrayList("<chat>");
}
case SERVER -> {
case SERVER:
return Lists.newArrayList("<server>");
}
}
}
}
case "remove" -> {
case "remove":
if (args.length == 2) {
return Lists.newArrayList("<npc_id>");
}
if (args.length == 3) {
return Lists.newArrayList("<action_id>");
}
}
case "cooldown" -> {
case "cooldown":
if (args.length == 2) {
return Lists.newArrayList("<npc_id>");
}
Expand All @@ -482,12 +473,10 @@ public List<String> actionTabComplete(CommandSender sender, String[] args) {
if (args.length == 4) {
return Lists.newArrayList("<delay_in_seconds>");
}
}
case "list" -> {
case "list":
if (args.length == 2) {
return Lists.newArrayList("<npc_id>");
}
}
}
return Lists.newArrayList();
}
Expand Down Expand Up @@ -594,7 +583,7 @@ public List<String> customizeTabComplete(CommandSender sender, String[] args) {
if (foundNPC == null)
return Lists.newArrayList();
NPCType npcType = foundNPC.getNpcPojo().getNpcType();
return partialTabCompleteOptions(args[1], npcType.getCustomizationLoader().getMethods().keySet().stream().toList());
return partialTabCompleteOptions(args[1], new ArrayList<>(npcType.getCustomizationLoader().getMethods().keySet()));
}
if (args.length == 3) {
NPC foundNPC = NPC.find(Integer.parseInt(args[1]));
Expand Down Expand Up @@ -819,11 +808,11 @@ public List<String> conversationTabComplete(CommandSender sender, String[] args)
if (args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("set")) {
Integer id = Ints.tryParse(args[1]);
if (id == null) {
return List.of();
return Collections.emptyList();
}
NPC npc = NPC.find(id);
if (npc == null) {
return List.of();
return Collections.emptyList();
}

return Lists.newArrayList(npc.getNpcPojo().getConversation() == null ? "null" : npc.getNpcPojo().getConversation().getConversationName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.github.znetworkw.znpcservers.exception;

import java.io.Serial;

/**
* @author xCodiq - 20/04/2023
*/
public class CommandException extends Exception {
@Serial private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

public CommandException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public InventoryListener(Plugin serversNPC) {

@EventHandler
public void onClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) return;
if (!(event.getWhoClicked() instanceof Player)) return;
final Player player = (Player) event.getWhoClicked();
if (event.getCurrentItem() == null) return;
if (!(event.getInventory().getHolder() instanceof ZInventoryHolder holder)) return;
if (!(event.getInventory().getHolder() instanceof ZInventoryHolder)) return;
final ZInventoryHolder holder = (ZInventoryHolder) event.getInventory().getHolder();
event.setCancelled(true);

ZInventoryPage page = holder.getzInventory().getPage();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/io/github/znetworkw/znpcservers/npc/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class NPC {
private static final ConcurrentMap<Integer, NPC> NPC_MAP = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -231,7 +232,7 @@ public synchronized void spawn(ZUser user) {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnPlayer(entityID,
this.gameProfile.getId(), SpigotConversionUtil.fromBukkitLocation(location.toBukkitLocation())));
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityMetadata(entityID,
List.of(new EntityData(NPCSkin.SkinLayerValues.findLayerByVersion(), EntityDataTypes.BYTE, Byte.MAX_VALUE))));
Collections.singletonList(new EntityData(NPCSkin.SkinLayerValues.findLayerByVersion(), EntityDataTypes.BYTE, Byte.MAX_VALUE))));
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityHeadLook(entityID, location.getYaw()));
});
}
Expand All @@ -243,7 +244,7 @@ player, new WrapperPlayServerSpawnEntity(entityID, Optional.ofNullable(uuid), ty
location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty()));
else PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnLivingEntity(
entityID, uuid, type, location.toVector3d(), location.getYaw(), location.getPitch(),
location.getPitch(), new Vector3d(), List.of()));
location.getPitch(), new Vector3d(), Collections.emptyList()));
}

if (FunctionFactory.isTrue(this, "holo")) this.hologram.spawn(user);
Expand Down Expand Up @@ -327,10 +328,10 @@ public void sendEquipPackets(ZUser zUser) {
.filter(entry -> Objects.nonNull(entry.getKey()))
.filter(entry -> Objects.nonNull(entry.getValue()))
.map(entry -> new Equipment(entry.getKey(), SpigotConversionUtil.fromBukkitItemStack(entry.getValue())))
.toList();
.collect(Collectors.toList());
if (equipment.size() == 0) return;
if (Utils.versionNewer(16)) PacketEvents.getAPI().getPlayerManager().sendPacket(zUser.toPlayer(), new WrapperPlayServerEntityEquipment(entityID, equipment));
else for (Equipment e : equipment) PacketEvents.getAPI().getPlayerManager().sendPacket(zUser.toPlayer(), new WrapperPlayServerEntityEquipment(entityID, List.of(e)));
else for (Equipment e : equipment) PacketEvents.getAPI().getPlayerManager().sendPacket(zUser.toPlayer(), new WrapperPlayServerEntityEquipment(entityID, Collections.singletonList(e)));
}

public void setPath(NPCPath.AbstractTypeWriter typeWriter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CompletableFuture<JsonObject> doReadSkin(SkinFetcherResult skinFetcherRes
if (this.builder.isUrlType()) {
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.writeBytes("url=" + URLEncoder.encode(this.builder.getData(), StandardCharsets.UTF_8));
outputStream.writeBytes("url=" + URLEncoder.encode(this.builder.getData(), "UTF-8"));
}
}
try (Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)) {
Expand Down

0 comments on commit 271b087

Please sign in to comment.