Skip to content

Commit

Permalink
Merge pull request #203 from utarwyn/feature/minecraft-1.19
Browse files Browse the repository at this point in the history
Support Minecraft 1.19
  • Loading branch information
utarwyn authored Jun 9, 2022
2 parents ce2aeab + 0dd2f11 commit 132e085
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h4 align="center">
A modern Bukkit plugin to divide enderchest into multiple inventories.
<br>
Works with Bukkit/Spigot 1.8 to 1.18!
Works with Bukkit/Spigot 1.8 to 1.19!
</h4>

<p align="center">
Expand Down
12 changes: 4 additions & 8 deletions dependencies/PlotSquared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<name>EnderContainers Dependency PlotSquared</name>

<properties>
<plotsquared.version>5.13.3</plotsquared.version>
<plotsquared.version>5.13.11</plotsquared.version>
<worldedit.version>7.2.3</worldedit.version>
<prtree.version>1.7.0-SNAPSHOT</prtree.version>
<prtree.version>1.7.0</prtree.version>
</properties>

<repositories>
Expand All @@ -26,12 +26,8 @@
<url>https://maven.enginehub.org/repo/</url>
</repository>
<repository>
<id>plotsquared-releases</id>
<url>https://mvn.intellectualsites.com/content/repositories/releases/</url>
</repository>
<repository>
<id>plotsquared-snapshots</id>
<url>https://mvn.intellectualsites.com/content/repositories/snapshots/</url>
<id>utarwyn</id>
<url>https://repo.utarwyn.fr</url>
</repository>
</repositories>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.utarwyn.endercontainers.compatibility;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
Expand Down Expand Up @@ -125,17 +124,19 @@ public static String enchantmentToString(Enchantment enchantment) {
}

public static Enchantment enchantmentFromString(String value) {
if (!StringUtils.isNumeric(value)) { // Name or NamespacedKey
try {
// Legacy method: use the name (old versions of Bukkit)
int numericValue = Integer.parseInt(value);
return Enchantment.getByName(ENCHANT_BY_IDS.get(numericValue));
} catch (NumberFormatException e) {
// Name or NamespacedKey
if (ServerVersion.isNewerThan(V1_12) && value.contains("!")) {
// New method to get en enchantment!
String[] parts = value.split("!");
return Enchantment.getByKey(new NamespacedKey(parts[0], parts[1]));
} else {
// Legacy method: use the name (old versions of Bukkit)
return Enchantment.getByName(value);
}
} else { // Legacy support for enchant ids
return Enchantment.getByName(ENCHANT_BY_IDS.get(Integer.valueOf(value)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum ServerVersion {
V1_15,
V1_16,
V1_17,
V1_18;
V1_18,
V1_19;

private static ServerVersion currentVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ private NMSHologramUtil() throws ReflectiveOperationException {
Class<?> armorStandClass = getNMSClass("EntityArmorStand", "world.entity.decoration");
Class<?> entityPlayerClass = getNMSClass("EntityPlayer", "server.level");
Class<?> destroyPacketClass = getNMSClass("PacketPlayOutEntityDestroy", "network.protocol.game");
Class<?> spawnPacketClass = getNMSClass("PacketPlayOutSpawnEntityLiving", "network.protocol.game");

// 1.19+ :: PacketPlayOutSpawnEntityLiving has been renamed PacketPlayOutSpawnEntity
String spawnPacketClassName = ServerVersion.isNewerThan(ServerVersion.V1_18)
? "PacketPlayOutSpawnEntity" : "PacketPlayOutSpawnEntityLiving";
Class<?> spawnPacketClass = getNMSClass(spawnPacketClassName, "network.protocol.game");

this.packetClass = getNMSClass("Packet", "network.protocol");
this.entityClass = getNMSClass("Entity", "world.entity");
Expand Down Expand Up @@ -257,8 +261,10 @@ private Object createHologramEntity(World w, double x, double y, double z, Strin
Method fromStringOrNullMethod = this.chatMessageClass.getMethod("fromStringOrNull", String.class);
Object chatComponent = fromStringOrNullMethod.invoke(null, text);

Method setCustomName = getNMSDynamicMethod(entityObject.getClass(), "setCustomName", "a", this.chatBaseComponentClass);
setCustomName.invoke(entityObject, chatComponent);
// 1.19+ :: method is now "b" (instead of "a")
String methodNamePost17 = ServerVersion.isNewerThan(ServerVersion.V1_18) ? "b" : "a";
getNMSDynamicMethod(entityObject.getClass(), "setCustomName", methodNamePost17, this.chatBaseComponentClass)
.invoke(entityObject, chatComponent);
} else {
Method setCustomName = entityObject.getClass().getMethod("setCustomName", String.class);
setCustomName.invoke(entityObject, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ private NMSPlayerUtil() throws ReflectiveOperationException {
getBukkitEntityMethod = entityPlayerClass.getDeclaredMethod("getBukkitEntity");
worldServer = this.prepareWorldServer(minecraftServerClass);

// 1.17+ :: we do not have to pass PlayerInteractManager to entity player constructor
if (ServerVersion.isNewerThan(ServerVersion.V1_16)) {
if (ServerVersion.isNewerThan(ServerVersion.V1_18)) {
// 1.19+ :: constructor also need a public key
Class<?> publicKeyClass = getNMSClass("ProfilePublicKey", "world.entity.player");
entityPlayerConstructor = entityPlayerClass.getDeclaredConstructor(
minecraftServerClass, worldServerClass, gameProfileClass, publicKeyClass
);
} else if (ServerVersion.isNewerThan(ServerVersion.V1_16)) {
// 1.17+ :: we do not have to pass PlayerInteractManager to entity player constructor
entityPlayerConstructor = entityPlayerClass.getDeclaredConstructor(
minecraftServerClass, worldServerClass, gameProfileClass
);
Expand Down Expand Up @@ -102,7 +108,12 @@ public Player loadPlayer(OfflinePlayer offline) throws ReflectiveOperationExcept

// 1.17+ :: we do not have to pass PlayerInteractManager to entity player constructor
if (playerInteractManager == null) {
entityPlayer = entityPlayerConstructor.newInstance(minecraftServer, worldServer, gameProfile);
// 1.19+ :: constructor also need a public key
if (ServerVersion.isNewerThan(ServerVersion.V1_18)) {
entityPlayer = entityPlayerConstructor.newInstance(minecraftServer, worldServer, gameProfile, null);
} else {
entityPlayer = entityPlayerConstructor.newInstance(minecraftServer, worldServer, gameProfile);
}
} else {
entityPlayer = entityPlayerConstructor.newInstance(minecraftServer, worldServer, gameProfile, playerInteractManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ protected static Class<?> getNMSClass(String className, String package1_17)

/**
* Get an internal net Minecraft Server method with a name changes.
* Usefull from Minecraft 1.18 because of minifier.
* Useful since Minecraft 1.18 because of minifiers.
*
* @param clazz class where the method is located
* @param name name used before Minecraft 1.18
* @param name18 name introduced with Minecraft 1.18
* @param namePost17 name introduced after Minecraft 1.17
* @param parameterTypes parameter types
* @return located method
* @throws NoSuchMethodException thrown if method has not been found
*/
protected static Method getNMSDynamicMethod(Class<?> clazz, String name, String name18, Class<?>... parameterTypes)
throws NoSuchMethodException {
return clazz.getMethod(ServerVersion.isNewerThan(ServerVersion.V1_17) ? name18 : name, parameterTypes);
protected static Method getNMSDynamicMethod(
Class<?> clazz, String name, String namePost17, Class<?>... parameterTypes
) throws NoSuchMethodException {
return clazz.getMethod(ServerVersion.isNewerThan(ServerVersion.V1_17) ? namePost17 : name, parameterTypes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.utarwyn.endercontainers.database.request;

import com.google.common.base.Joiner;
import fr.utarwyn.endercontainers.database.Database;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;

Expand Down Expand Up @@ -83,7 +83,7 @@ public String getRequest() {
// Construct conditions if exists
if (this.conditions.length > 0) {
sb.append(" WHERE ");
sb.append(StringUtils.join(this.conditions, " AND "));
sb.append(Joiner.on(" AND ").join(this.conditions));
}

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.utarwyn.endercontainers.database.request;

import com.google.common.base.Joiner;
import fr.utarwyn.endercontainers.database.Database;
import fr.utarwyn.endercontainers.database.DatabaseManager;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -130,12 +130,12 @@ public String getRequest() {
}
request.deleteCharAt(request.length() - 1);

request.append(" WHERE ").append(StringUtils.join(this.conditions, " AND "));
request.append(" WHERE ").append(Joiner.on(" AND ").join(this.conditions));
} else {
String action = this.replaceIfExists ? "REPLACE" : "INSERT";

request.append(action).append(" INTO `").append(this.table).append('`');
request.append("(").append(StringUtils.join(this.fields, ",")).append(")");
request.append("(").append(Joiner.on(',').join(this.fields)).append(")");

request.append(" VALUES ");
request.append("(").append(this.generateFakeParameters(this.values)).append(")");
Expand All @@ -145,7 +145,7 @@ public String getRequest() {
}

private String generateFakeParameters(Object[] values) {
return StringUtils.join(Arrays.stream(values).map(v -> '?').toArray(), ",");
return Joiner.on(',').join(Arrays.stream(values).map(v -> '?').toArray());
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.utarwyn.endercontainers.database.request;

import com.google.common.base.Joiner;
import fr.utarwyn.endercontainers.database.Database;
import fr.utarwyn.endercontainers.database.DatabaseManager;
import fr.utarwyn.endercontainers.database.DatabaseSet;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -119,9 +119,9 @@ public List<DatabaseSet> findAll() throws SQLException {
public String getRequest() {
StringBuilder request = new StringBuilder("SELECT ");

request.append(StringUtils.join(this.fields, ","));
request.append(Joiner.on(',').join(this.fields));
request.append(" FROM ");
request.append(StringUtils.join(this.froms, ","));
request.append(Joiner.on(',').join(this.froms));

for (String[] join : this.joins) {
request.append(" JOIN ").append(join[0]).append(" ON ").append(join[1]).append(" = ").append(join[2]);
Expand All @@ -132,13 +132,13 @@ public String getRequest() {
}

if (this.conditions.length > 0) {
request.append(" WHERE ").append(StringUtils.join(this.conditions, " AND "));
request.append(" WHERE ").append(Joiner.on(" AND ").join(this.conditions));
}
if (this.groupsBy.length > 0) {
request.append(" GROUP BY ").append(StringUtils.join(this.groupsBy, ","));
request.append(" GROUP BY ").append(Joiner.on(',').join(this.groupsBy));
}
if (this.orders.length > 0) {
request.append(" ORDER BY ").append(StringUtils.join(this.orders, ","));
request.append(" ORDER BY ").append(Joiner.on(',').join(this.orders));
}
if (this.limits.length > 0) {
if (this.limits[1] > 0) { // limites
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.utarwyn.endercontainers.storage.serialization;

import fr.utarwyn.endercontainers.compatibility.CompatibilityHelper;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -140,9 +139,9 @@ public ConcurrentMap<Integer, ItemStack> deserialize(String data) {
Material material;

// Material ids is an old way to store items, now we use material names (more reliable).
if (StringUtils.isNumeric(value)) {
try {
material = CompatibilityHelper.materialFromId(Integer.parseInt(value));
} else {
} catch (NumberFormatException e) {
material = CompatibilityHelper.matchMaterial(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.utarwyn.endercontainers.util;

import com.google.common.base.Strings;
import fr.utarwyn.endercontainers.EnderContainers;
import fr.utarwyn.endercontainers.configuration.Files;
import fr.utarwyn.endercontainers.configuration.LocaleKey;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -132,8 +132,8 @@ public static void accessDenied(CommandSender receiver) {
* @param receiver receiver of the message
*/
public static void pluginBar(CommandSender receiver) {
String pBar = "§5§m" + StringUtils.repeat("-", 5);
String sBar = "§d§m" + StringUtils.repeat("-", 11);
String pBar = "§5§m" + Strings.repeat("-", 5);
String sBar = "§d§m" + Strings.repeat("-", 11);

receiver.sendMessage("§8++" + pBar + sBar + "§r§d( §6EnderContainers §d)" + sBar + pBar + "§8++");
}
Expand All @@ -144,8 +144,8 @@ public static void pluginBar(CommandSender receiver) {
* @param receiver receiver of the message
*/
public static void endBar(CommandSender receiver) {
String pBar = "§5§m" + StringUtils.repeat("-", 5);
receiver.sendMessage("§8++" + pBar + "§d§m" + StringUtils.repeat("-", 39) + pBar + "§8++");
String pBar = "§5§m" + Strings.repeat("-", 5);
receiver.sendMessage("§8++" + pBar + "§d§m" + Strings.repeat("-", 39) + pBar + "§8++");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.utarwyn.endercontainers.util;

import com.google.common.base.Objects;
import org.apache.commons.lang.Validate;
import com.google.common.base.Preconditions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class SemanticVersion implements Comparable<SemanticVersion> {
* @param version semantic version in a string format
*/
public SemanticVersion(String version) {
Validate.notNull(version, "semantic version cannot be null");
Preconditions.checkArgument(version != null, "semantic version cannot be null");
Matcher matcher = Pattern.compile(REGEX).matcher(version);

if (!matcher.matches()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,22 @@ public void playerJoinUpdateNotification() throws ReflectiveOperationException {
Updater updater = mock(Updater.class);
TestHelper.registerManagers(updater);

when(this.player.getLocation()).thenReturn(mock(Location.class));

// no permission
this.listener.onPlayerJoin(event);
verify(this.player, never()).playSound(any(), any(Sound.class), eq(1f), eq(1f));
verify(this.player, never()).playSound(any(Location.class), any(Sound.class), eq(1f), eq(1f));

// no update
when(this.player.isOp()).thenReturn(true);
when(updater.notifyPlayer(this.player)).thenReturn(false);
this.listener.onPlayerJoin(event);
verify(this.player, never()).playSound(any(), any(Sound.class), eq(1f), eq(1f));
verify(this.player, never()).playSound(any(Location.class), any(Sound.class), eq(1f), eq(1f));

// update and permission
when(updater.notifyPlayer(this.player)).thenReturn(true);
this.listener.onPlayerJoin(event);
verify(this.player).playSound(any(), eq(Sound.BLOCK_NOTE_BLOCK_PLING), eq(1f), eq(1f));
verify(this.player).playSound(any(Location.class), eq(Sound.BLOCK_NOTE_BLOCK_PLING), eq(1f), eq(1f));
}

@Test
Expand Down Expand Up @@ -241,19 +243,19 @@ public void inventoryCloseUnsupportedActions() {
// try with an unknown entity -> no sound
when(event.getPlayer()).thenReturn(mock(HumanEntity.class));
this.listener.onInventoryClose(event);
verify(this.player, never()).playSound(any(), any(Sound.class), anyFloat(), anyFloat());
verify(this.player, never()).playSound(any(Location.class), any(Sound.class), anyFloat(), anyFloat());

// try with another type of container -> no sound
when(event.getPlayer()).thenReturn(this.player);
when(event.getInventory().getType()).thenReturn(InventoryType.CHEST);
this.listener.onInventoryClose(event);
verify(this.player, never()).playSound(any(), any(Sound.class), anyFloat(), anyFloat());
verify(this.player, never()).playSound(any(Location.class), any(Sound.class), anyFloat(), anyFloat());

// try with an enderchest managed by the plugin -> no sound (integrated in the inventory system)
when(event.getInventory().getType()).thenReturn(InventoryType.ENDER_CHEST);
when(this.manager.getVanillaEnderchestUsedBy(this.player)).thenReturn(Optional.empty());
this.listener.onInventoryClose(event);
verify(this.player, never()).playSound(any(), any(Sound.class), anyFloat(), anyFloat());
verify(this.player, never()).playSound(any(Location.class), any(Sound.class), anyFloat(), anyFloat());
}

private PlayerInteractEvent createInteractEvent(Action action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public Color getDefaultLeatherColor() {
return Color.fromRGB(10511680);
}

@Override
public ItemStack createItemStack(String input) throws IllegalArgumentException {
return null;
}

@Override
public Material updateMaterial(ItemMeta meta, Material material)
throws IllegalArgumentException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ public boolean removeAttributeModifier(Attribute attribute, AttributeModifier at
return false;
}

@Override
public String getAsString() {
return "";
}

@Override
public CustomItemTagContainer getCustomTagContainer() {
throw new UnsupportedOperationException();
Expand Down
Loading

0 comments on commit 132e085

Please sign in to comment.