Skip to content

Commit

Permalink
Merge pull request #47 from DaJokni/master
Browse files Browse the repository at this point in the history
packet handling, version check & other improvements
  • Loading branch information
Chasewhip8 authored May 26, 2024
2 parents fe2714f + 3384342 commit d3bf1df
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 138 deletions.
55 changes: 36 additions & 19 deletions src/main/java/net/crashcraft/crashclaim/CrashClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import net.crashcraft.crashclaim.crashutils.menusystem.GUI;
import net.crashcraft.crashclaim.data.ClaimDataManager;
import net.crashcraft.crashclaim.data.MaterialName;
import net.crashcraft.crashclaim.listeners.PaperListener;
import net.crashcraft.crashclaim.listeners.PlayerListener;
import net.crashcraft.crashclaim.listeners.WorldListener;
import net.crashcraft.crashclaim.localization.LocalizationLoader;
import net.crashcraft.crashclaim.migration.MigrationManager;
import net.crashcraft.crashclaim.nms.NMSHandler;
import net.crashcraft.crashclaim.nms.NMSManager;
import net.crashcraft.crashclaim.packet.PacketHandler;
import net.crashcraft.crashclaim.permissions.PermissionHelper;
import net.crashcraft.crashclaim.pluginsupport.PluginSupport;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportManager;
Expand All @@ -50,7 +48,7 @@ public class CrashClaim extends JavaPlugin {

private CrashClaimAPI api;

private NMSHandler handler;
private PacketHandler handler;
private PluginSupportManager pluginSupport;

private ClaimDataManager manager;
Expand Down Expand Up @@ -81,21 +79,18 @@ public void onLoad() {

@Override
public void onEnable() {

String minecraftVersion = Bukkit.getMinecraftVersion();
if (!minecraftVersion.equals("1.20.6")) {
getLogger().severe("Incompatible server version: " + minecraftVersion);
if (!isServerSupported("1.20.6")){
getServer().getPluginManager().disablePlugin(this);
return;
}

Bukkit.getPluginManager().registerEvents(pluginSupport, this);

taskChainFactory = BukkitTaskChainFactory.create(this);
this.adventure = BukkitAudiences.create(this);

loadConfigs();

handler = new NMSManager().getHandler(); // Find and fetch version wrapper
handler = new PacketHandler(); // Find and fetch version wrapper

getLogger().info("Loading language file");
LocalizationLoader.initialize(); // Init and reload localization
Expand Down Expand Up @@ -124,14 +119,6 @@ public void onEnable() {
PacketListenerPriority.LOW);
PacketEvents.getAPI().init();

if (PaperLib.isPaper()) {
getLogger().info("Using extra protections provided by the paper api");
Bukkit.getPluginManager().registerEvents(new PaperListener(manager, visualizationManager), this);
} else {
getLogger().info("Looks like your not running paper, some protections will be disabled");
PaperLib.suggestPaper(this);
}

pluginSupport.onEnable();
LocalizationLoader.register(); // Register PlaceHolders

Expand Down Expand Up @@ -213,6 +200,36 @@ public void loadConfigs(){
}
}

private boolean isServerSupported(String supportedVersion) {
if (!PaperLib.isPaper()){
getLogger().severe("CrashClaim requires Paper to run.");
PaperLib.suggestPaper(this);
getServer().getPluginManager().disablePlugin(this);
return false;
}
String minecraftVersion = Bukkit.getMinecraftVersion();
int supportedVersionInt = versionStringToInt(supportedVersion);
int minecraftVersionInt = versionStringToInt(minecraftVersion);

if (minecraftVersionInt != supportedVersionInt) {
if (minecraftVersionInt > supportedVersionInt) {
getLogger().severe("Your server's version is newer than CrashClaim's minimum supported version, which is " + supportedVersion +
". The plugin will still attempt to load, but issues may arise. " +
"Please check if the plugin has newer versions available. Your server is currently running version " + minecraftVersion + ".");
return true;
} else {
getLogger().severe("Your server's version is older than CrashClaim's minimum supported version, which is " + supportedVersion +
". Your server is currently running version " + minecraftVersion + ".");
return false;
}
}
return true;
}

private int versionStringToInt(String version) {
return Integer.parseInt(version.replace(".", ""));
}

public void disablePlugin(String error){
getLogger().severe(error);
Bukkit.getPluginManager().disablePlugin(this);
Expand Down Expand Up @@ -267,7 +284,7 @@ public BukkitAudiences getAdventure() {
return adventure;
}

public NMSHandler getHandler() {
public PacketHandler getHandler() {
return handler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@ public class CrashUtils implements Listener {

public CrashUtils(JavaPlugin javaPlugin){
plugin = javaPlugin;

PaperLib.suggestPaper(javaPlugin);
}

public void setupMenuSubSystem(){
Bukkit.getServer().getPluginManager().registerEvents(new CrashMenuController(plugin), plugin); //Gui controller
}

public void setupTextureCache(){
if (PaperLib.isPaper()) {
if (textureCache == null) {
textureCache = new TextureCache();
Bukkit.getServer().getPluginManager().registerEvents(textureCache, plugin);
}
} else {
plugin.getLogger().severe("Your server is not running Paper or a Paper derivative, texture caching has not been enabled");
if (textureCache == null) {
textureCache = new TextureCache();
Bukkit.getServer().getPluginManager().registerEvents(textureCache, plugin);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.crashcraft.crashclaim.listeners;

import com.destroystokyo.paper.MaterialTags;
import com.destroystokyo.paper.event.entity.ThrownEggHatchEvent;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.claimobjects.BaseClaim;
import net.crashcraft.crashclaim.claimobjects.Claim;
Expand Down Expand Up @@ -709,4 +710,22 @@ public void onBlockIgniteEvent(BlockIgniteEvent e){
}
}
}

@EventHandler (priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onThrownEggHatchEvent(ThrownEggHatchEvent e){
if (!e.isHatching()){
return;
}

if (e.getEgg().getShooter() instanceof Player player) {
if (!helper.hasPermission(player.getUniqueId(), e.getEgg().getLocation(), PermissionRoute.ENTITIES)){
e.setHatching(false);
visuals.sendAlert(player, Localization.ALERT__NO_PERMISSIONS__ENTITIES.getMessage(player));
}
} else {
if (!helper.hasPermission(e.getEgg().getLocation(), PermissionRoute.ENTITIES)){
e.setHatching(false);
}
}
}
}
20 changes: 0 additions & 20 deletions src/main/java/net/crashcraft/crashclaim/nms/NMSManager.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.crashcraft.crashclaim.nms;
package net.crashcraft.crashclaim.packet;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
Expand All @@ -9,21 +9,12 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerTeams;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class NMSHandler {

public void sendActionBar(Player player, BaseComponent[] message) {
player.sendActionBar(message);
}

public class PacketHandler {

public void setEntityTeam(Player player, String team, List<String> uuids){
WrapperPlayServerTeams.ScoreBoardTeamInfo teamInfo = new WrapperPlayServerTeams.ScoreBoardTeamInfo(
Expand All @@ -36,16 +27,9 @@ public void setEntityTeam(Player player, String team, List<String> uuids){

}

public int getMinWorldHeight(World world) {
return world.getMinHeight();
}

public void removeEntity(Player player, Set<Integer> entity_ids){
WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(entity_ids.stream().mapToInt(Integer::intValue).toArray());

PacketEvents.getAPI().getPlayerManager().sendPacket(player, packet);


}

public void spawnGlowingInvisibleMagmaSlime(Player player, double x, double z, double y, int id, UUID uuid,
Expand All @@ -65,25 +49,4 @@ public void spawnGlowingInvisibleMagmaSlime(Player player, double x, double z, d
fakeEntities.put(id, uuid.toString());
entityLocations.put(id, new Location(player.getWorld(), x, y, z));
}

private AtomicInteger ENTITY_ID;

private String NMS;

public Class<?> getNMSClass(final String className) throws ClassNotFoundException {
return Class.forName(NMS + className);
}

public int getUniqueEntityID() {
if (ENTITY_ID == null) {
try {
final Field entityCount = Class.forName("net.minecraft.world.entity.Entity").getDeclaredField("c");
entityCount.setAccessible(true);
ENTITY_ID = (AtomicInteger) entityCount.get(null);
} catch (final ReflectiveOperationException e) {
throw new IllegalArgumentException(e);
}
}
return ENTITY_ID.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void disable() {
@Override
public boolean canClaim(Location minLoc, Location maxLoc){
ProtectedRegion test = new ProtectedCuboidRegion("dummy",
BlockVector3.at(minLoc.getX(), CrashClaim.getPlugin().getHandler().getMinWorldHeight(minLoc.getWorld()), minLoc.getZ()),
BlockVector3.at(minLoc.getX(), minLoc.getWorld().getMinHeight(), minLoc.getZ()),
BlockVector3.at(maxLoc.getX(), maxLoc.getWorld().getMaxHeight(), maxLoc.getZ())
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,7 @@ public void sendAlert(Player player, BaseComponent[] message){
if (message == null || message.length < 1){
return;
}

CrashClaim.getPlugin().getHandler().sendActionBar(player,
message
/* unused
GlobalConfig.visual_alert_fade_in,
GlobalConfig.visual_alert_duration,
GlobalConfig.visual_alert_fade_out
*/
);
player.sendActionBar(message);
}

public VisualGroup fetchExistingGroup(UUID uuid){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.visualize.VisualizationManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void removeAllVisualsOfType(VisualType type){
}

public int generateUniqueID(){
return CrashClaim.getPlugin().getHandler().getUniqueEntityID();
return Bukkit.getServer().getUnsafe().nextEntityId();
}

public UUID generateUniqueUUID(){
Expand Down

0 comments on commit d3bf1df

Please sign in to comment.