Skip to content

Commit

Permalink
Attempted support for 1.17 & 1.18 with version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Realizedd committed Jan 3, 2022
1 parent 275248f commit 187711c
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 246 deletions.
42 changes: 25 additions & 17 deletions arenaregen/build.gradle
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.apache.tools.ant.filters.ReplaceTokens

clean.doFirst {
delete "$rootDir/out/"
}

tasks.withType(ShadowJar) {
destinationDir = file("$rootDir/out/")
}

processResources {
duplicatesStrategy = DuplicatesStrategy.INCLUDE

from(sourceSets.main.resources.srcDirs) {
include '**/*.yml'
filter(ReplaceTokens, tokens: [VERSION: project.version])
}
}

dependencies {
compile 'org.projectlombok:lombok:1.16.20'
compile 'org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT'
compile 'com.github.Realizedd.Duels:duels-api:3.4.1'
compile project(':nms')
compile project(':v1_8_R3')
compile project(':v1_8_R3_paper')
compile project(':v1_12_R1')
compile project(':v1_14_R1')
compile project(':v1_15_R1')
compile project(':v1_16_R3')
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
implementation 'org.spigotmc:spigot-api:1.13-R0.1-SNAPSHOT'
implementation 'com.github.Realizedd.Duels:duels-api:3.4.1'
implementation project(':nms')
implementation project(':v1_8_R3')
implementation project(':v1_8_R3_paper')
implementation project(':v1_12_R1')
implementation project(':v1_14_R1')
implementation project(':v1_15_R1')
implementation project(':v1_16_R3')
implementation project(':v1_17_R1')
implementation project(':v1_18_R1')
}

shadowJar {
archiveName = parent.name.replace("Parent", "") + '-' + project.version + '.jar'
getDestinationDirectory().set(file("$rootDir/out/"))

final String archiveName = parent.name.replace("Parent", "") + '-' + project.version + '.jar'
getArchiveFileName().set(archiveName)

dependencies {
include(dependency(':nms'))
Expand All @@ -40,7 +43,12 @@ shadowJar {
include(dependency(':v1_14_R1'))
include(dependency(':v1_15_R1'))
include(dependency(':v1_16_R3'))
include(dependency(':v1_17_R1'))
include(dependency(':v1_18_R1'))
}
}

build.dependsOn(shadowJar)
// To build extension jar, run './gradlew clean build'.
build {
dependsOn(shadowJar)
}
36 changes: 24 additions & 12 deletions arenaregen/src/main/java/me/realized/de/arenaregen/ArenaRegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,27 @@ public class ArenaRegen extends DuelsExtension {
public void onEnable() {
this.configuration = new Config(this);
this.lang = new Lang(this);
this.handler = findHandler();

info("NMSHandler: Using " + handler.getClass().getName());

this.selectionManager = new SelectionManager(this, api);
this.zoneManager = new ResetZoneManager(this, api);
api.registerSubCommand("duels", new ArenaregenCommand(this, api));
}

private NMS findHandler() {
final String handlerVersion = configuration.getBlockResetHandlerVersion();
final String packageName = api.getServer().getClass().getPackage().getName();
final String version = packageName.substring(packageName.lastIndexOf('.') + 1);
String version = packageName.substring(packageName.lastIndexOf('.') + 1);

if (!handlerVersion.equalsIgnoreCase("auto")) {
if (handlerVersion.equalsIgnoreCase("fallback")) {
return new NMSHandler();
}

version = handlerVersion;
}

try {
Class<?> clazz = null;
Expand All @@ -41,19 +59,13 @@ public void onEnable() {
}

if (clazz == null) {
clazz = Class.forName("me.realized.de.arenaregen.nms." + version + ".NMSHandler");
clazz = ReflectionUtil.getClassUnsafe("me.realized.de.arenaregen.nms." + version + ".NMSHandler");
}

this.handler = NMS.class.isAssignableFrom(clazz) ? (NMS) clazz.newInstance() : new NMSHandler();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
this.handler = new NMSHandler();
return clazz != null && NMS.class.isAssignableFrom(clazz) ? (NMS) clazz.newInstance() : new NMSHandler();
} catch (IllegalAccessException | InstantiationException ex) {
return new NMSHandler();
}

info("NMSHandler: Using " + handler.getClass().getName());

this.selectionManager = new SelectionManager(this, api);
this.zoneManager = new ResetZoneManager(this, api);
api.registerSubCommand("duels", new ArenaregenCommand(this, api));
}

@Override
Expand All @@ -63,7 +75,7 @@ public void onDisable() {

@Override
public String getRequiredVersion() {
return "3.4.1";
return "3.5.0";
}

public void info(final String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import me.realized.duels.api.arena.Arena;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ResetCommand extends ARCommand {

Expand All @@ -34,10 +33,6 @@ public void execute(final CommandSender sender, final String label, final String

lang.sendMessage(sender, "COMMAND.arenaregen.reset.start", "name", name);
zone.reset(() -> {
if (sender instanceof Player) {
zone.refreshChunks((Player) sender);
}

lang.sendMessage(sender, "COMMAND.arenaregen.reset.end", "name", name);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.realized.de.arenaregen.config;

import java.util.Collections;
import java.util.List;
import lombok.Getter;
import me.realized.de.arenaregen.ArenaRegen;
import org.bukkit.Material;
Expand All @@ -14,8 +16,12 @@ public class Config {
@Getter
private final boolean allowArenaBlockBreak;
@Getter
private final String blockResetHandlerVersion;
@Getter
private final boolean removeDroppedItems;
@Getter
private final List<String> removeEntities;
@Getter
private final boolean preventBlockBurn;
@Getter
private final boolean preventBlockMelt;
Expand All @@ -31,7 +37,9 @@ public Config(final ArenaRegen extension) {
this.selectingTool = Material.getMaterial(config.getString("selecting-tool", "IRON_AXE"));
this.allowArenaBlockBreak = config.getBoolean("allow-arena-block-break", false);
this.blocksPerTick = config.getInt("blocks-per-tick", 25);
blockResetHandlerVersion = config.getString("block-reset-handler-version", "auto");
this.removeDroppedItems = config.getBoolean("remove-dropped-items", true);
this.removeEntities = config.isList("remove-entities") ? config.getStringList("remove-entities") : Collections.emptyList();
this.preventBlockBurn = config.getBoolean("prevent-block-burn", true);
this.preventBlockMelt = config.getBoolean("prevent-block-melt", true);
this.preventBlockExplode = config.getBoolean("prevent-block-explode", true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,163 +1,33 @@
package me.realized.de.arenaregen.nms.fallback;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import me.realized.de.arenaregen.nms.NMS;
import me.realized.de.arenaregen.util.CompatUtil;
import me.realized.de.arenaregen.util.ReflectionUtil;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;

public class NMSHandler implements NMS {

private Constructor<?> BLOCK_POS_CONSTRUCTOR;
private Object BLOCK_ENUM;
private Method WORLD_GET_HANDLE;
private Method WORLD_C;
private Method WORLD_UPDATE_LIGHTING;
private Method CHUNK_GET_HANDLE;
private Method BLOCK_FROM_LEGACY_DATA;
private Method BLOCK_GET_DATA;
private Method MAGIC_GET_BLOCK;
private Method CHUNK_SET_BLOCK_12;
private Method CHUNK_SET_BLOCK;

private Method GET_CHUNK_PROVIDER;
private Method GET_LIGHT_ENGINE;
private Method LIGHT_ENGINE_A;

private Method GET_HANDLE;
private Field PLAYER_CONNECTION;
private Method SEND_PACKET;

private Constructor<?> PACKET_MAP_CHUNK_CONSTRUCTOR;
private Method SET_DATA;

public NMSHandler() {
try {
final Class<?> BLOCK_POS = ReflectionUtil.getNMSClass("BlockPosition");
BLOCK_POS_CONSTRUCTOR = ReflectionUtil.getConstructor(BLOCK_POS, Double.TYPE, Double.TYPE, Double.TYPE);
WORLD_GET_HANDLE = ReflectionUtil.getMethod(ReflectionUtil.getCBClass("CraftWorld"), "getHandle");
final Class<?> WORLD = ReflectionUtil.getNMSClass("World");
final Class<?> ENUM_SKY_BLOCK = ReflectionUtil.getNMSClass("EnumSkyBlock");
WORLD_C = ReflectionUtil.getMethod(WORLD, "c", ENUM_SKY_BLOCK, BLOCK_POS);
GET_CHUNK_PROVIDER = ReflectionUtil.getMethod(WORLD, "getChunkProvider");
final Class<?> CHUNK_PROVIDER = ReflectionUtil.getNMSClass("IChunkProvider");
GET_LIGHT_ENGINE = ReflectionUtil.getMethod(CHUNK_PROVIDER, "getLightEngine");
final Class<?> LIGHT_ENGINE = ReflectionUtil.getNMSClass("LightEngine");
LIGHT_ENGINE_A = ReflectionUtil.getMethod(LIGHT_ENGINE, "a", BLOCK_POS);

if (CompatUtil.isPaper()) {
WORLD_UPDATE_LIGHTING = ReflectionUtil.getMethod(WORLD, "updateLight", ENUM_SKY_BLOCK, BLOCK_POS);
}

BLOCK_ENUM = ReflectionUtil.getEnumConstant(ENUM_SKY_BLOCK, 1);
CHUNK_GET_HANDLE = ReflectionUtil.getMethod(ReflectionUtil.getCBClass("CraftChunk"), "getHandle");
final Class<?> BLOCK = ReflectionUtil.getNMSClass("Block");
BLOCK_FROM_LEGACY_DATA = ReflectionUtil.getMethod(BLOCK, "fromLegacyData", Integer.TYPE);
BLOCK_GET_DATA = ReflectionUtil.getMethod(BLOCK, "getBlockData");
MAGIC_GET_BLOCK = ReflectionUtil.getMethod(ReflectionUtil.getCBClass("util.CraftMagicNumbers"), "getBlock", Material.class);
final Class<?> CHUNK = ReflectionUtil.getNMSClass("Chunk");
final Class<?> BLOCK_DATA = ReflectionUtil.getNMSClass("IBlockData");
CHUNK_SET_BLOCK_12 = ReflectionUtil.getMethod(CHUNK, "a", BLOCK_POS, BLOCK_DATA);
CHUNK_SET_BLOCK = ReflectionUtil.getMethod(CHUNK, "a", BLOCK_POS, BLOCK_DATA, Boolean.TYPE);

if (CHUNK_SET_BLOCK == null) {
CHUNK_SET_BLOCK = ReflectionUtil.getMethod(CHUNK, "setType", BLOCK_POS, BLOCK_DATA, Boolean.TYPE);
}

final Class<?> CB_PLAYER = ReflectionUtil.getCBClass("entity.CraftPlayer");
GET_HANDLE = ReflectionUtil.getMethod(CB_PLAYER, "getHandle");

final Class<?> NMS_PLAYER = ReflectionUtil.getNMSClass("EntityPlayer");
PLAYER_CONNECTION = ReflectionUtil.getField(NMS_PLAYER, "playerConnection");
SEND_PACKET = ReflectionUtil.getMethod(ReflectionUtil.getNMSClass("PlayerConnection"), "sendPacket", ReflectionUtil.getNMSClass("Packet"));

final Class<?> PACKET_MAP_CHUNK = ReflectionUtil.getNMSClass("PacketPlayOutMapChunk");
PACKET_MAP_CHUNK_CONSTRUCTOR = ReflectionUtil.getConstructor(PACKET_MAP_CHUNK, CHUNK, boolean.class, int.class);

if (PACKET_MAP_CHUNK_CONSTRUCTOR == null) {
PACKET_MAP_CHUNK_CONSTRUCTOR = ReflectionUtil.getConstructor(PACKET_MAP_CHUNK, CHUNK, int.class);
}
} catch (Throwable ex) {
ex.printStackTrace();
}
SET_DATA = ReflectionUtil.getMethod(Block.class, "setData", byte.class);
}

@Override
public void sendChunkUpdate(final Player player, final Chunk chunk) {
try {
final Object chunkHandle = CHUNK_GET_HANDLE.invoke(chunk);
final Object connection = PLAYER_CONNECTION.get(GET_HANDLE.invoke(player));

if (CompatUtil.isPre1_12()) {
SEND_PACKET.invoke(connection, PACKET_MAP_CHUNK_CONSTRUCTOR.newInstance(chunkHandle, true, 65535));
} else {
SEND_PACKET.invoke(connection, PACKET_MAP_CHUNK_CONSTRUCTOR.newInstance(chunkHandle, 65535));
}
} catch (IllegalAccessException | InvocationTargetException | InstantiationException ex) {
ex.printStackTrace();
}
}
public void sendChunkUpdate(final Player player, final Chunk chunk) {}

@Override
public void setBlockFast(final Block block, final Material material, final int data) {
final Chunk chunk = block.getChunk();
final int x = block.getX();
final int y = block.getY();
final int z = block.getZ();

try {
final Object chunkHandle = CHUNK_GET_HANDLE.invoke(chunk);
Object nmsBlock = MAGIC_GET_BLOCK.invoke(null, material);

final Object blockPos = BLOCK_POS_CONSTRUCTOR.newInstance(x, y, z);
block.setType(material);

if (CompatUtil.isPre1_13()) {
nmsBlock = BLOCK_FROM_LEGACY_DATA.invoke(nmsBlock, data);
CHUNK_SET_BLOCK_12.invoke(chunkHandle, blockPos, nmsBlock);
} else {
CHUNK_SET_BLOCK.invoke(chunkHandle, blockPos, BLOCK_GET_DATA.invoke(nmsBlock), true);
}

if (block.getType() == Material.AIR || isSurrounded(block.getWorld(), x, y, z)) {
return;
}

final Object worldHandle = WORLD_GET_HANDLE.invoke(block.getWorld());

if (CompatUtil.isPre1_14()) {
if (CompatUtil.isPaper() && WORLD_UPDATE_LIGHTING != null) {
WORLD_UPDATE_LIGHTING.invoke(worldHandle, BLOCK_ENUM, blockPos);
} else {
WORLD_C.invoke(worldHandle, BLOCK_ENUM, blockPos);
}
} else {
LIGHT_ENGINE_A.invoke(GET_LIGHT_ENGINE.invoke(GET_CHUNK_PROVIDER.invoke(worldHandle)), blockPos);
}
} catch (Throwable ex) {
ex.printStackTrace();
if (SET_DATA != null) {
try {
SET_DATA.invoke(block, (byte) data);
} catch (IllegalAccessException | InvocationTargetException ignored) {}
}
}

private static boolean isSurrounded(final World world, final int x, final int y, final int z) {
final Block base = world.getBlockAt(x, y, z);
final Block east = base.getRelative(BlockFace.EAST);
final Block west = base.getRelative(BlockFace.WEST);
final Block south = base.getRelative(BlockFace.SOUTH);
final Block north = base.getRelative(BlockFace.NORTH);
final Block up = base.getRelative(BlockFace.UP);
final Block down = base.getRelative(BlockFace.DOWN);
return !east.getType().isTransparent()
&& !west.getType().isTransparent()
&& !up.getType().isTransparent()
&& !down.getType().isTransparent()
&& !south.getType().isTransparent()
&& !north.getType().isTransparent();
}
}
Loading

0 comments on commit 187711c

Please sign in to comment.