[ci skip] Reformat all files

This commit is contained in:
FlorianMichael committed 2025-10-28 20:18:48 +01:00
1 parent 2494d08d3e
commit 91b9f6d9b7
11 files changed
+107 -113

No files matched your search

@@ -19,15 +19,15 @@
package com.viaversion.viarewind.legacysupport;
import com.viaversion.viarewind.legacysupport.feature.AnvilCostFix;
import com.viaversion.viarewind.legacysupport.feature.BlockCollisionChanges;
import com.viaversion.viarewind.legacysupport.versioninfo.VersionInformer;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viarewind.legacysupport.feature.AreaEffectCloudEmulator;
import com.viaversion.viarewind.legacysupport.feature.SlimeBounceEmulator;
import com.viaversion.viarewind.legacysupport.feature.BlockCollisionChanges;
import com.viaversion.viarewind.legacysupport.feature.BlockPlaceSoundEmulator;
import com.viaversion.viarewind.legacysupport.feature.BrewingInteractionEmulator;
import com.viaversion.viarewind.legacysupport.feature.ElytraVelocityEmulator;
import com.viaversion.viarewind.legacysupport.feature.EnchantingGuiEmulator;
import com.viaversion.viarewind.legacysupport.feature.BlockPlaceSoundEmulator;
import com.viaversion.viarewind.legacysupport.feature.SlimeBounceEmulator;
import com.viaversion.viarewind.legacysupport.versioninfo.VersionInformer;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
@@ -19,6 +19,7 @@ package com.viaversion.viarewind.legacysupport.feature;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viarewind.legacysupport.util.NMSUtil;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
@@ -27,7 +28,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.Inventory;
import java.util.logging.Level;
public class AnvilCostFix implements Listener {
@EventHandler
@@ -18,10 +18,13 @@
package com.viaversion.viarewind.legacysupport.feature;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viarewind.legacysupport.util.NMSUtil;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
@@ -31,10 +34,6 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.LingeringPotionSplashEvent;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;
@SuppressWarnings("unchecked")
public class AreaEffectCloudEmulator implements Listener {
private final ArrayList<AreaEffectCloud> effectClouds = new ArrayList<>();
@@ -47,8 +46,8 @@ public class AreaEffectCloudEmulator implements Listener {
final Location location = cloud.getLocation();
final float radius = cloud.getRadius();
final Object data = NMSUtil.NEWER_THAN_V1_20_5
? cloud.getColor()
: null;
? cloud.getColor()
: null;
float area = (float) Math.PI * radius * radius;
@@ -68,11 +67,11 @@ public class AreaEffectCloudEmulator implements Listener {
affectedPlayers.stream().filter(player -> player.getWorld() == location.getWorld()).forEach(player -> {
player.spawnParticle(
cloud.getParticle(),
location.getX() + f3, location.getY(), location.getZ() + f6,
0,
r / 255f, g / 255f, b / 255f,
data
cloud.getParticle(),
location.getX() + f3, location.getY(), location.getZ() + f6,
0,
r / 255f, g / 255f, b / 255f,
data
);
});
}
@@ -20,7 +20,6 @@ package com.viaversion.viarewind.legacysupport.feature;
import com.viaversion.viarewind.legacysupport.util.ReflectionUtil;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -31,8 +30,8 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.viaversion.viarewind.legacysupport.util.ReflectionUtil.*;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.*;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.getNMSBlockClass;
import static com.viaversion.viarewind.legacysupport.util.ReflectionUtil.getFieldAccessible;
public class BlockCollisionChanges {
@@ -79,8 +78,7 @@ public class BlockCollisionChanges {
field.setAccessible(true);
final Object value = field.get(null);
if (value instanceof Map<?, ?>) {
@SuppressWarnings("unchecked")
final Map<Object, Object> casted = (Map<Object, Object>) value;
@SuppressWarnings("unchecked") final Map<Object, Object> casted = (Map<Object, Object>) value;
shapesMap = casted;
break;
}
@@ -98,8 +96,7 @@ public class BlockCollisionChanges {
if (!updated) {
throw new IllegalStateException("Could not adjust ladder shapes for modern versions");
}
} else
{
} else {
final boolean pre1_12_2 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_12_2);
final boolean pre1_13_2 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_13_2);
final boolean pre1_16_4 = serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_16_4);
@@ -200,7 +197,7 @@ public class BlockCollisionChanges {
final Field isEmpty = getFieldAccessible(voxelShape, "isEmpty");
isEmpty.setBoolean(voxelShapeArray, true);
final Method initCache = ReflectionUtil.findMethod(voxelShape, new String[]{ "initCache", "moonrise$initCache" });
final Method initCache = ReflectionUtil.findMethod(voxelShape, new String[]{"initCache", "moonrise$initCache"});
if (initCache == null) {
throw new IllegalStateException("Could not find initCache method in " + voxelShape.getName());
}
@@ -269,7 +266,7 @@ public class BlockCollisionChanges {
continue;
}
if (!voxelShapeInterface.isAssignableFrom(shapeObject.getClass())
&& !shapeObject.getClass().getSimpleName().contains("VoxelShape")) {
&& !shapeObject.getClass().getSimpleName().contains("VoxelShape")) {
continue;
}
@@ -398,8 +395,8 @@ public class BlockCollisionChanges {
if (blockBoxMethod != null) {
final double scale = 16.0D;
return blockBoxMethod.invoke(null,
values[0] * scale, values[1] * scale, values[2] * scale,
values[3] * scale, values[4] * scale, values[5] * scale);
values[0] * scale, values[1] * scale, values[2] * scale,
values[3] * scale, values[4] * scale, values[5] * scale);
}
throw new IllegalStateException("Unable to create voxel shape for ladder bounding box.");
}
@@ -18,9 +18,11 @@
package com.viaversion.viarewind.legacysupport.feature;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import java.lang.reflect.Method;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
@@ -36,11 +38,11 @@ import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import java.lang.reflect.Method;
import java.util.logging.Level;
import static com.viaversion.viarewind.legacysupport.util.ReflectionUtil.*;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.*;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.getBlockPositionClass;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.getGamePacketClass;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.getSoundCategoryClass;
import static com.viaversion.viarewind.legacysupport.util.NMSUtil.sendPacket;
import static com.viaversion.viarewind.legacysupport.util.ReflectionUtil.getMethod;
@SuppressWarnings("unchecked")
public class BlockPlaceSoundEmulator implements Listener {
@@ -81,52 +83,20 @@ public class BlockPlaceSoundEmulator implements Listener {
}
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent e) {
final Player player = e.getPlayer();
if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_9)) return;
if (Via.getAPI().getServerVersion().lowestSupportedProtocolVersion().newerThanOrEqualTo(ProtocolVersion.v1_17)) {
player.playSound(e.getBlockPlaced().getLocation(), e.getBlock().getBlockData().getSoundGroup().getPlaceSound(), 1.0f, 0.8f);
} else {
try {
playBlockPlaceSoundNMS(player, e.getBlock());
} catch (Exception exception) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Could not play block place sound.", exception);
}
}
}
private void onItemPickUp(Player player) {
final float volume = 0.2f;
final float pitch = (float) ((Math.random() - Math.random()) * 0.7f + 1.0f) * 2.0f;
playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, volume, pitch);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onExperienceOrbPickup(PlayerExpChangeEvent e) {
final float volume = 0.1f;
final float pitch = (float) (0.5f * ((Math.random() - Math.random()) * 0.7f + 1.8f));
playSound(e.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, volume, pitch);
}
private static void playSound(final Location loc, final Sound sound, final float volume, final float pitch) {
Bukkit.getOnlinePlayers().stream()
.filter(p -> p.getWorld() == loc.getWorld())
.filter(p -> p.getLocation().distanceSquared(loc) < (double) 16 * (double) 16)
.filter(p -> Via.getAPI().getPlayerVersion(p) <= 47)
.forEach(p -> {
if (isSoundCategory) {
p.playSound(loc, sound, SoundCategory.valueOf("PLAYERS"), volume, pitch);
} else {
p.playSound(loc, sound, volume, pitch);
}
});
.filter(p -> p.getWorld() == loc.getWorld())
.filter(p -> p.getLocation().distanceSquared(loc) < (double) 16 * (double) 16)
.filter(p -> Via.getAPI().getPlayerVersion(p) <= 47)
.forEach(p -> {
if (isSoundCategory) {
p.playSound(loc, sound, SoundCategory.valueOf("PLAYERS"), volume, pitch);
} else {
p.playSound(loc, sound, volume, pitch);
}
});
}
// 1.8.8 -> 1.16.5
private static void playBlockPlaceSoundNMS(Player player, Block block) throws Exception {
World world = block.getWorld();
@@ -198,13 +168,13 @@ public class BlockPlaceSoundEmulator implements Listener {
private static void playSound(Player player, Object soundEffect, Object soundCategory, double x, double y, double z, float volume, float pitch) {
try {
Object packet = getGamePacketClass("PacketPlayOutNamedSoundEffect").getConstructor(
soundEffect.getClass(), soundCategory.getClass(),
double.class, double.class, double.class,
float.class, float.class
soundEffect.getClass(), soundCategory.getClass(),
double.class, double.class, double.class,
float.class, float.class
).newInstance(
soundEffect, soundCategory,
x, y, z,
volume, pitch
soundEffect, soundCategory,
x, y, z,
volume, pitch
);
// Volume = 1
@@ -214,4 +184,35 @@ public class BlockPlaceSoundEmulator implements Listener {
ex.printStackTrace();
}
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent e) {
final Player player = e.getPlayer();
if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_9)) return;
if (Via.getAPI().getServerVersion().lowestSupportedProtocolVersion().newerThanOrEqualTo(ProtocolVersion.v1_17)) {
player.playSound(e.getBlockPlaced().getLocation(), e.getBlock().getBlockData().getSoundGroup().getPlaceSound(), 1.0f, 0.8f);
} else {
try {
playBlockPlaceSoundNMS(player, e.getBlock());
} catch (Exception exception) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Could not play block place sound.", exception);
}
}
}
private void onItemPickUp(Player player) {
final float volume = 0.2f;
final float pitch = (float) ((Math.random() - Math.random()) * 0.7f + 1.0f) * 2.0f;
playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, volume, pitch);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onExperienceOrbPickup(PlayerExpChangeEvent e) {
final float volume = 0.1f;
final float pitch = (float) (0.5f * ((Math.random() - Math.random()) * 0.7f + 1.8f));
playSound(e.getPlayer().getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, volume, pitch);
}
}
@@ -23,6 +23,8 @@ import com.viaversion.viarewind.legacysupport.util.NMSUtil;
import com.viaversion.viarewind.legacysupport.util.ReflectionUtil;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import java.lang.reflect.Method;
import java.util.logging.Level;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -30,9 +32,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector;
import java.lang.reflect.Method;
import java.util.logging.Level;
@SuppressWarnings("unchecked")
public class ElytraVelocityEmulator implements Listener {
@@ -20,6 +20,7 @@ package com.viaversion.viarewind.legacysupport.feature;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@@ -31,8 +32,6 @@ import org.bukkit.inventory.EnchantingInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.Map;
@SuppressWarnings("unchecked")
public class EnchantingGuiEmulator implements Listener {
@@ -31,26 +31,27 @@ import org.bukkit.util.Vector;
@SuppressWarnings({"unchecked", "DataFlowIssue"})
public class SlimeBounceEmulator implements Listener {
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
if (e.getTo().getY() >= e.getFrom().getY()) return; // Only check upwards motion
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
if (e.getTo().getY() >= e.getFrom().getY()) return; // Only check upwards motion
final Player player = e.getPlayer();
if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_8)) return; // Only apply for 1.7 and below players
final Player player = e.getPlayer();
if (Via.getAPI().getPlayerProtocolVersion(player).newerThanOrEqualTo(ProtocolVersion.v1_8))
return; // Only apply for 1.7 and below players
if (Math.floor(e.getTo().getY()) + 0.01 < e.getTo().getY()) return;
if (player.isSneaking()) return;
if (Math.floor(e.getTo().getY()) + 0.01 < e.getTo().getY()) return;
if (player.isSneaking()) return;
final Block block = e.getTo().clone().add(0, -0.1, 0).getBlock();
if (block.getType() != Material.SLIME_BLOCK) return;
final Block block = e.getTo().clone().add(0, -0.1, 0).getBlock();
if (block.getType() != Material.SLIME_BLOCK) return;
final Vector newVelocity = player.getVelocity();
final Vector newVelocity = player.getVelocity();
final double deltaY = (e.getTo().getY() - e.getFrom().getY());
if (deltaY >- 0.11) return; // Only bounce if the player is falling down
final double deltaY = (e.getTo().getY() - e.getFrom().getY());
if (deltaY > -0.11) return; // Only bounce if the player is falling down
newVelocity.setY(-deltaY * 1.05);
newVelocity.setY(-deltaY * 1.05);
player.setVelocity(newVelocity);
}
player.setVelocity(newVelocity);
}
}
@@ -19,23 +19,21 @@ package com.viaversion.viarewind.legacysupport.util;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import static com.viaversion.viarewind.legacysupport.util.ReflectionUtil.failSafeGetClass;
public class NMSUtil {
public static final boolean NEWER_THAN_V1_20_5 = BukkitPlugin.getInstance().getServerProtocol().newerThanOrEqualTo(ProtocolVersion.v1_20_5);
public static String nmsVersionPackage;
private static Field playerConnectionField;
public static final boolean NEWER_THAN_V1_20_5 = BukkitPlugin.getInstance().getServerProtocol().newerThanOrEqualTo(ProtocolVersion.v1_20_5);
static {
if (BukkitPlugin.getInstance().getServerProtocol().olderThan(ProtocolVersion.v1_17)) {
nmsVersionPackage = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
@@ -130,7 +128,7 @@ public class NMSUtil {
return;
}
try {
final Method sendPacket = ReflectionUtil.findMethod(playerConnection.getClass(), new String[] {"sendPacket", "a"}, getPacketClass());
final Method sendPacket = ReflectionUtil.findMethod(playerConnection.getClass(), new String[]{"sendPacket", "a"}, getPacketClass());
sendPacket.invoke(playerConnection, packet);
} catch (IllegalAccessException | InvocationTargetException | NullPointerException e) {
BukkitPlugin.getInstance().getLogger().log(Level.SEVERE, "Failed to send packet to player", e);
@@ -18,7 +18,6 @@
package com.viaversion.viarewind.legacysupport.util;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -80,7 +79,8 @@ public class ReflectionUtil {
Field field = null;
try {
field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException ignored) {} // Cache non-existing field too
} catch (NoSuchFieldException ignored) {
} // Cache non-existing field too
fieldCache.put(key, field);
return field;
}
@@ -18,8 +18,8 @@
package com.viaversion.viarewind.legacysupport.versioninfo;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viarewind.legacysupport.BukkitPlugin;
import com.viaversion.viaversion.api.Via;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
@@ -35,7 +35,7 @@ public class VersionInformer implements Listener {
public VersionInformer(final BukkitPlugin plugin, final FileConfiguration config) {
final String message = ChatColor.translateAlternateColorCodes('&', config.getString("versioninfo.message")).
replace("%version%", Bukkit.getVersion().split(" ")[2].replace(")", ""));
replace("%version%", Bukkit.getVersion().split(" ")[2].replace(")", ""));
this.versionMessage = message.split(System.lineSeparator());
maxVersion = config.getInt("versioninfo.max-version");