Skip to content

Commit

Permalink
Support 1.9 using ProtocolLib (#1199)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 authored Aug 7, 2023
1 parent 7fa2a4d commit 51ac791
Show file tree
Hide file tree
Showing 33 changed files with 2,027 additions and 150 deletions.
1 change: 1 addition & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<include>org.slf4j:slf4j-api:*</include>
<include>fr.minuskube.inv:smart-invs</include>
<include>net.objecthunter:exp4j:*</include>
<include>io.github.bananapuncher714:nbteditor:*</include>
</includes>
</artifactSet>
<filters>
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/tc/oc/pgm/PGMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final class PGMConfig implements Config {
// ui.*
private final boolean showSideBar;
private final boolean showTabList;
private final boolean resizeTabList;
private final boolean showTabListPing;
private final boolean showProximity;
private final boolean showFireworks;
Expand Down Expand Up @@ -183,6 +184,7 @@ public final class PGMConfig implements Config {
this.showProximity = parseBoolean(config.getString("ui.proximity", "false"));
this.showSideBar = parseBoolean(config.getString("ui.sidebar", "true"));
this.showTabList = parseBoolean(config.getString("ui.tablist", "true"));
this.resizeTabList = parseBoolean(config.getString("ui.tablist-resize", "false"));
this.showTabListPing = parseBoolean(config.getString("ui.ping", "true"));
this.participantsSeeObservers =
parseBoolean(config.getString("ui.participants-see-observers", "true"));
Expand Down Expand Up @@ -573,6 +575,11 @@ public boolean showTabList() {
return showTabList;
}

@Override
public boolean resizeTabList() {
return resizeTabList;
}

@Override
public boolean showTabListPing() {
return showTabListPing;
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/tc/oc/pgm/PGMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import tc.oc.pgm.util.listener.ItemTransferListener;
import tc.oc.pgm.util.listener.PlayerBlockListener;
import tc.oc.pgm.util.listener.PlayerMoveListener;
import tc.oc.pgm.util.nms.NMSHacks;
import tc.oc.pgm.util.tablist.TablistResizer;
import tc.oc.pgm.util.text.TextException;
import tc.oc.pgm.util.text.TextTranslations;
import tc.oc.pgm.util.xml.InvalidXMLException;
Expand Down Expand Up @@ -114,6 +116,9 @@ public void onEnable() {
return; // Indicates the plugin failed to load, so exit early
}

// Sanity test PGM is running on a supported version before doing any work
NMSHacks.allocateEntityId();

Permissions.registerAll();

final CommandSender console = getServer().getConsoleSender();
Expand Down Expand Up @@ -214,6 +219,14 @@ public void onEnable() {

if (config.showTabList()) {
matchTabManager = new MatchTabManager(this);

if (config.resizeTabList()) {
if (this.getServer().getPluginManager().isPluginEnabled("ProtocolLib")) {
TablistResizer.registerAdapter(this);
} else {
logger.warning("ProtocolLib is required when 'ui.resize' is enabled");
}
}
}

if (!config.getUptimeLimit().isNegative()) {
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ public interface Config {
*/
boolean showTabList();

/**
* Gets whether the tab list should be resized to 4 rows for 1.7 players.
*
* @return If the tab list will be resized.
*/
boolean resizeTabList();

/**
* Gets whether the tab list is should show real ping.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public void show(MatchPlayer player) {
spawn(bukkit, base(player));
segments.forEach(segment -> spawn(bukkit, segment));
range(1, segments.size())
.forEachOrdered(i -> segments.get(i - 1).ride(bukkit, segments.get(i).entity()));
base(player).ride(bukkit, segments.get(0).entity());
.forEachOrdered(i -> segments.get(i - 1).ride(bukkit, segments.get(i).entityId()));
base(player).ride(bukkit, segments.get(0).entityId());

update(player);
}
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/tc/oc/pgm/shield/ShieldPlayerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public void remove() {
addAbsorption(-shieldHealth);
}

static Sound RECHARGE_SOUND = resolveRechaseSound();

static Sound resolveRechaseSound() {
try {
return Sound.ORB_PICKUP;
} catch (Throwable t) {
return Sound.valueOf("ENTITY_EXPERIENCE_ORB_PICKUP");
}
}

/**
* Recharge the shield to its maximum health. If the player has more absorption than the current
* shield strength, the excess is preserved.
Expand All @@ -66,7 +76,7 @@ void recharge() {
logger.fine("Recharging shield: shield=" + shieldHealth + " delta=" + delta);
shieldHealth = parameters.maxHealth;
addAbsorption(delta);
bukkit.playSound(bukkit.getLocation(), Sound.ORB_PICKUP, 1, 2);
bukkit.playSound(bukkit.getLocation(), RECHARGE_SOUND, 1, 2);
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ gameplay:
ui:
sidebar: true # Enable the side bar?
tablist: true # Enable the tab list?
tablist-resize: false # Resize the tab list for 1.7 players? Requires ProtocolLib
ping: false # Should tab list show real ping?
proximity: false # Should the proximity of objectives be visible?
fireworks: true # Spawn fireworks after objectives are completed?
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PGM
softdepend: [ViaVersion]
softdepend: [ViaVersion, ProtocolLib]
description: ${project.parent.description}
main: ${project.mainClass}
version: ${project.version} (git-${git.commit.id.abbrev})
Expand Down
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
<id>pgm-fyi</id>
<url>https://repo.pgm.fyi/snapshots</url>
</repository>
<repository> <!-- Protocol Lib -->
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository> <!-- NBT Editing without NMS -->
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
</repository>
</repositories>

<distributionManagement>
Expand Down Expand Up @@ -98,6 +106,13 @@
</exclusions>
</dependency>

<!-- Send packets to players -->
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.0.0</version>
</dependency>

<!-- XML parsing library used for all "map.xml" configuration loading -->
<dependency>
<groupId>org.jdom</groupId>
Expand Down Expand Up @@ -173,6 +188,13 @@
<version>1.2.1</version>
<scope>compile</scope>
</dependency>

<!-- Small reflection library for editing NBT -->
<dependency>
<groupId>io.github.bananapuncher714</groupId>
<artifactId>nbteditor</artifactId>
<version>7.18.5</version>
</dependency>

<!--Framework for creating interactive GUIs - https://github.com/MinusKube/SmartInvs -->
<dependency>
Expand Down
8 changes: 8 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/attribute/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public enum Attribute {
GENERIC_MOVEMENT_SPEED("generic.movementSpeed"),
/** Attack damage of an Entity. */
GENERIC_ATTACK_DAMAGE("generic.attackDamage"),
/** Attack speed of an Entity */
GENERIC_ATTACK_SPEED("generic.attackSpeed"),
/** Armor of an Entity */
GENERIC_ARMOR("generic.armor"),
/** Armor Toughness of an Entity */
GENERIC_ARMOR_TOUGHNESS("generic.armorToughness"),
/** Luck */
GENERIC_LUCK("generic.luck"),
/** Strength with which a horse will jump. */
HORSE_JUMP_STRENGTH("horse.jumpStrength"),
/** Chance of a zombie to spawn reinforcements. */
Expand Down
2 changes: 1 addition & 1 deletion util/src/main/java/tc/oc/pgm/util/bukkit/BukkitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static Plugin getPlugin() {
return PLUGIN.get();
}

Boolean isSportPaper = Bukkit.getServer().getVersion().contains("SportPaper");
Boolean isSportPaper = Platform.SERVER_PLATFORM == Platform.SPORTPAPER_1_8;

static boolean isSportPaper() {
return isSportPaper;
Expand Down
95 changes: 95 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/bukkit/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package tc.oc.pgm.util.bukkit;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Bukkit;
import tc.oc.pgm.util.ClassLogger;
import tc.oc.pgm.util.nms.NMSHacksNoOp;
import tc.oc.pgm.util.nms.NMSHacksPlatform;
import tc.oc.pgm.util.nms.v1_8.NMSHacksSportPaper;
import tc.oc.pgm.util.nms.v1_9.NMSHacks1_9;
import tc.oc.pgm.util.reflect.ReflectionUtils;

public enum Platform {
UNKNOWN("UNKNOWN", "UNKNOWN", NMSHacksNoOp.class, false),
SPORTPAPER_1_8("SportPaper", "1.8", NMSHacksSportPaper.class, false),
SPIGOT_1_8(
"Spigot",
"1.8", // NMSHacks1_8 causes issues with other versions, get it dynamically
(Class<? extends NMSHacksPlatform>)
ReflectionUtils.getClassFromName("tc.oc.pgm.util.nms.v1_8.NMSHacks1_8"),
false),
PAPER_1_8(
"Paper",
"1.8", // NMSHacks1_8 causes issues with other versions, get it dynamically
(Class<? extends NMSHacksPlatform>)
ReflectionUtils.getClassFromName("tc.oc.pgm.util.nms.v1_8.NMSHacks1_8"),
false),
SPIGOT_1_9("Spigot", "1.9", NMSHacks1_9.class, true),
PAPER_1_9("Paper", "1.9", NMSHacks1_9.class, true);

private static ClassLogger logger = ClassLogger.get(Platform.class);;
public static Platform SERVER_PLATFORM = computeServerPlatform();

private static Platform computeServerPlatform() {
String versionString = Bukkit.getServer().getVersion();
for (Platform platform : Platform.values()) {
if (versionString.contains(platform.variant)
&& versionString.contains("MC: " + platform.majorVersion)) {
return platform;
}
}
return UNKNOWN;
}

private final String variant;
private final String majorVersion;
private final Class<? extends NMSHacksPlatform> nmsHacksClass;
private final boolean requiresProtocolLib;

Platform(
String variant,
String majorVersion,
Class<? extends NMSHacksPlatform> nmsHacksClass,
boolean requiresProtocolLib) {
this.variant = variant;
this.majorVersion = majorVersion;
this.nmsHacksClass = nmsHacksClass;
this.requiresProtocolLib = requiresProtocolLib;
}

public NMSHacksPlatform getNMSHacks() {
if (this == UNKNOWN) {
Bukkit.getServer().getPluginManager().disablePlugin(BukkitUtils.getPlugin());
throw new UnsupportedOperationException("UNKNOWN Platform!");
}
if (this.requiresProtocolLib
&& !Bukkit.getServer().getPluginManager().isPluginEnabled("ProtocolLib")) {
Bukkit.getServer().getPluginManager().disablePlugin(BukkitUtils.getPlugin());
throw new UnsupportedOperationException(
"ProtocolLib is required for PGM to run on " + this.toString());
}
if (this == SERVER_PLATFORM) {
try {
logger.info("Detected server: " + this.toString());
Constructor<? extends NMSHacksPlatform> constructor = nmsHacksClass.getConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
} catch (InvocationTargetException
| InstantiationException
| IllegalAccessException
| NoSuchMethodException e) {
Bukkit.getServer().getPluginManager().disablePlugin(BukkitUtils.getPlugin());
throw new RuntimeException(e);
}
} else {
Bukkit.getServer().getPluginManager().disablePlugin(BukkitUtils.getPlugin());
throw new UnsupportedOperationException("getNMSHacks called for incorrect platform!");
}
}

@Override
public String toString() {
return this.variant + " (" + this.majorVersion + ")";
}
}
34 changes: 11 additions & 23 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
Expand Down Expand Up @@ -34,37 +33,17 @@
import org.bukkit.scoreboard.NameTagVisibility;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.util.ClassLogger;
import tc.oc.pgm.util.attribute.AttributeMap;
import tc.oc.pgm.util.attribute.AttributeModifier;
import tc.oc.pgm.util.block.RayBlockIntersection;
import tc.oc.pgm.util.bukkit.BukkitUtils;
import tc.oc.pgm.util.bukkit.Platform;
import tc.oc.pgm.util.nms.entity.fake.FakeEntity;
import tc.oc.pgm.util.nms.entity.potion.EntityPotion;
import tc.oc.pgm.util.skin.Skin;

public interface NMSHacks {

NMSHacksPlatform INSTANCE = chooseNMSHacks();

static NMSHacksPlatform chooseNMSHacks() {
NMSHacksPlatform choice;
Logger logger = ClassLogger.get(NMSHacks.class);

try {
if (BukkitUtils.isSportPaper()) {
choice = new NMSHacksSportPaper();
logger.info("Using NMSHacksSportPaper");
} else {
choice = new NMSHacks1_8();
logger.info("Using NMSHacks1_8");
}
} catch (Throwable throwable) {
logger.severe("You are trying to run PGM on an unsupported version!");
throw throwable;
}
return choice;
}
NMSHacksPlatform INSTANCE = Platform.SERVER_PLATFORM.getNMSHacks();

AtomicInteger ENTITY_IDS = new AtomicInteger(Integer.MAX_VALUE);

Expand Down Expand Up @@ -317,10 +296,19 @@ static boolean teleportRelative(
return INSTANCE.teleportRelative(player, deltaPos, deltaYaw, deltaPitch, cause);
}

static void sendSpawnEntityPacket(
Player player, int entityId, Location location, Vector velocity) {
INSTANCE.sendSpawnEntityPacket(player, entityId, location, velocity);
}

static void spawnFreezeEntity(Player player, int entityId, boolean legacy) {
INSTANCE.spawnFreezeEntity(player, entityId, legacy);
}

static void spawnFakeArmorStand(Player player, int entityId, Location location, Vector velocity) {
INSTANCE.spawnFakeArmorStand(player, entityId, location, velocity);
}

/**
* Test if the given tool is capable of "efficiently" mining the given block.
*
Expand Down
Loading

0 comments on commit 51ac791

Please sign in to comment.