mirror of
https://github.com/ViaVersion/ViaRewind-Legacy-Support.git
synced 2024-11-14 10:15:31 +01:00
block place sounds
This commit is contained in:
parent
486b968e21
commit
b8e45fc953
@ -3,10 +3,12 @@ package de.gerrygames.viarewind.legacysupport;
|
||||
import de.gerrygames.viarewind.legacysupport.injector.LilyPadFixer;
|
||||
import de.gerrygames.viarewind.legacysupport.listener.BrewingListener;
|
||||
import de.gerrygames.viarewind.legacysupport.listener.EnchantingListener;
|
||||
import de.gerrygames.viarewind.legacysupport.listener.SoundListener;
|
||||
import de.gerrygames.viarewind.legacysupport.versioninfo.VersionInformer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
|
||||
|
||||
public class BukkitPlugin extends JavaPlugin {
|
||||
private static BukkitPlugin instance;
|
||||
@ -17,11 +19,18 @@ public class BukkitPlugin extends JavaPlugin {
|
||||
saveDefaultConfig();
|
||||
getConfig().options().copyDefaults(true);
|
||||
saveConfig();
|
||||
FileConfiguration config = getConfig();
|
||||
if (config.getBoolean("enchanting-gui-fix")) Bukkit.getPluginManager().registerEvents(new EnchantingListener(), this);
|
||||
if (config.getBoolean("brewing-stand-gui-fix")) Bukkit.getPluginManager().registerEvents(new BrewingListener(), this);
|
||||
if (config.getBoolean("lily-pad-fix")) LilyPadFixer.fix();
|
||||
if (config.getBoolean("versioninfo.active")) new VersionInformer();
|
||||
final FileConfiguration config = getConfig();
|
||||
Bukkit.getScheduler().runTask(this, () -> {
|
||||
if (ProtocolRegistry.SERVER_PROTOCOL>5 && config.getBoolean("enchanting-gui-fix"))
|
||||
Bukkit.getPluginManager().registerEvents(new EnchantingListener(), this);
|
||||
if (ProtocolRegistry.SERVER_PROTOCOL>78 && config.getBoolean("brewing-stand-gui-fix"))
|
||||
Bukkit.getPluginManager().registerEvents(new BrewingListener(), this);
|
||||
if (ProtocolRegistry.SERVER_PROTOCOL>84 && config.getBoolean("lily-pad-fix"))
|
||||
LilyPadFixer.fix();
|
||||
if (ProtocolRegistry.SERVER_PROTOCOL>47 && config.getBoolean("sound-fix"))
|
||||
Bukkit.getPluginManager().registerEvents(new SoundListener(), this);
|
||||
if (config.getBoolean("versioninfo.active")) new VersionInformer();
|
||||
});
|
||||
}
|
||||
|
||||
public static BukkitPlugin getInstance() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.gerrygames.viarewind.legacysupport.injector;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class NMSReflection {
|
||||
private static String version;
|
||||
@ -11,10 +12,29 @@ public class NMSReflection {
|
||||
|
||||
public static Class getNMSClass(String name) {
|
||||
try {
|
||||
return Class.forName("net.minecraft.server." + NMSReflection.getVersion() + "." + name);
|
||||
return Class.forName("net.minecraft.server." + getVersion() + "." + name);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Class getCraftBukkitClass(String name) {
|
||||
try {
|
||||
return Class.forName("org.bukkit.craftbukkit." + getVersion() + "." + name);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void sendPacket(Player player, Object packet) {
|
||||
try {
|
||||
Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player);
|
||||
Object playerConnection = nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer);
|
||||
playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +1,72 @@
|
||||
package de.gerrygames.viarewind.legacysupport.listener;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.BrewingStand;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.BrewerInventory;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
|
||||
public class BrewingListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryOpen(InventoryOpenEvent e) {
|
||||
if (!(e.getInventory() instanceof BrewerInventory)) return;
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerInteract(PlayerInteractEvent e) {
|
||||
if (!e.hasBlock() || e.getClickedBlock().getType()!=Material.BREWING_STAND) return;
|
||||
Player player = (Player) e.getPlayer();
|
||||
int version = Via.getAPI().getPlayerVersion(player);
|
||||
if (version>79) return;
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
ItemStack blazePowder = new ItemStack(Material.BLAZE_POWDER);
|
||||
int amount = 0;
|
||||
for (int i = 0; i<playerInventory.getSize(); i++) {
|
||||
ItemStack item = playerInventory.getItem(i);
|
||||
if (item==null || !item.isSimilar(blazePowder)) continue;
|
||||
if (amount + item.getAmount() > 64) {
|
||||
item.setAmount(amount + item.getAmount() - 64);
|
||||
amount = 64;
|
||||
ItemStack playerItem = e.getItem();
|
||||
if (playerItem==null) playerItem = new ItemStack(Material.AIR);
|
||||
BrewingStand brewingStand = (BrewingStand)e.getClickedBlock().getState();
|
||||
BrewerInventory inventory = brewingStand.getInventory();
|
||||
ItemStack fuel = inventory.getFuel();
|
||||
if (fuel==null) fuel = new ItemStack(Material.AIR);
|
||||
|
||||
if (e.getAction()==Action.RIGHT_CLICK_BLOCK) {
|
||||
if (!blazePowder.isSimilar(playerItem)) return;
|
||||
if (fuel.getType()!=Material.AIR && !fuel.isSimilar(playerItem)) return;
|
||||
if (fuel.getAmount()>=64) return;
|
||||
int amount = player.isSneaking() ? Math.min(playerItem.getAmount(), 64-fuel.getAmount()) : 1;
|
||||
if (playerItem.getAmount()==amount) {
|
||||
playerItem = new ItemStack(Material.AIR);
|
||||
} else {
|
||||
amount += item.getAmount();
|
||||
item = new ItemStack(Material.AIR);
|
||||
playerItem.setAmount(playerItem.getAmount()-amount);
|
||||
}
|
||||
playerInventory.setItem(i, item);
|
||||
if (amount==64) break;
|
||||
if (fuel.getType()==Material.AIR) {
|
||||
fuel = new ItemStack(Material.BLAZE_POWDER, amount);
|
||||
} else {
|
||||
fuel.setAmount(fuel.getAmount()+amount);
|
||||
}
|
||||
inventory.setFuel(fuel);
|
||||
} else {
|
||||
if (!blazePowder.isSimilar(fuel)) return;
|
||||
if (!blazePowder.isSimilar(playerItem) && playerItem.getType()!=Material.AIR) return;
|
||||
if (playerItem.getAmount()>=64) return;
|
||||
int amount = player.isSneaking() ? Math.min(fuel.getAmount(), 64-playerItem.getAmount()) : 1;
|
||||
if (fuel.getAmount()==amount) {
|
||||
fuel = new ItemStack(Material.AIR);
|
||||
} else {
|
||||
fuel.setAmount(fuel.getAmount()-amount);
|
||||
}
|
||||
if (playerItem.getType()==Material.AIR) {
|
||||
playerItem = new ItemStack(Material.BLAZE_POWDER, amount);
|
||||
} else {
|
||||
playerItem.setAmount(playerItem.getAmount()+amount);
|
||||
}
|
||||
inventory.setFuel(fuel);
|
||||
}
|
||||
if (amount==0) return;
|
||||
BrewerInventory inventory = (BrewerInventory) e.getInventory();
|
||||
blazePowder.setAmount(amount);
|
||||
inventory.setFuel(blazePowder);
|
||||
if (e.getHand()==EquipmentSlot.HAND) {
|
||||
player.getInventory().setItemInMainHand(playerItem);
|
||||
} else {
|
||||
player.getInventory().setItemInOffHand(playerItem);
|
||||
}
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,7 @@ public class EnchantingListener implements Listener {
|
||||
public void onInventoryOpen(InventoryOpenEvent e) {
|
||||
if (!(e.getInventory() instanceof EnchantingInventory)) return;
|
||||
Player player = (Player) e.getPlayer();
|
||||
int version = Via.getAPI().getPlayerVersion(player);
|
||||
if (version>5) return;
|
||||
if (Via.getAPI().getPlayerVersion(player)>5) return;
|
||||
PlayerInventory playerInventory = player.getInventory();
|
||||
ItemStack lapis = new ItemStack(Material.INK_SACK, 1, (short) 4);
|
||||
int amount = 0;
|
||||
|
@ -0,0 +1,58 @@
|
||||
package de.gerrygames.viarewind.legacysupport.listener;
|
||||
|
||||
import de.gerrygames.viarewind.legacysupport.injector.NMSReflection;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class SoundListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent e) {
|
||||
Player player = e.getPlayer();
|
||||
if (Via.getAPI().getPlayerVersion(player)>47) return;
|
||||
playBlockPlaceSound(player, e.getBlock());
|
||||
}
|
||||
|
||||
private static void playBlockPlaceSound(Player player, Block block) {
|
||||
try {
|
||||
World world = block.getWorld();
|
||||
Object nmsWorld = world.getClass().getMethod("getHandle").invoke(world);
|
||||
Class blockPositionClass = NMSReflection.getNMSClass("BlockPosition");
|
||||
Object blockPosition = blockPositionClass.getConstructor(int.class, int.class, int.class).newInstance(block.getX(), block.getY(), block.getZ());
|
||||
Object blockData = nmsWorld.getClass().getSuperclass().getMethod("getType", blockPositionClass).invoke(nmsWorld, blockPosition);
|
||||
Method getBlock = blockData.getClass().getMethod("getBlock");
|
||||
getBlock.setAccessible(true);
|
||||
Object nmsBlock = getBlock.invoke(blockData);
|
||||
Object soundType = nmsBlock.getClass().getMethod("getStepSound").invoke(nmsBlock);
|
||||
|
||||
Object soundEffect = soundType.getClass().getMethod("e").invoke(soundType);
|
||||
float f1 = (float) soundType.getClass().getMethod("a").invoke(soundType);
|
||||
float f2 = (float) soundType.getClass().getMethod("a").invoke(soundType);
|
||||
Object soundCategory = Enum.class.getMethod("valueOf", Class.class, String.class).invoke(null, NMSReflection.getNMSClass("SoundCategory"), "BLOCKS");
|
||||
|
||||
f1 = (f1 + 1.0f) / 2.0f;
|
||||
f2 *= 0.8;
|
||||
|
||||
Object packet = NMSReflection.getNMSClass("PacketPlayOutNamedSoundEffect").getConstructor(
|
||||
soundEffect.getClass(), soundCategory.getClass(),
|
||||
double.class, double.class, double.class,
|
||||
float.class, float.class)
|
||||
.newInstance(
|
||||
soundEffect, soundCategory,
|
||||
block.getX() + 0.5, block.getY() + 0.5, block.getZ() + 0.5,
|
||||
f1, f2
|
||||
);
|
||||
|
||||
NMSReflection.sendPacket(player, packet);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -2,8 +2,10 @@
|
||||
brewing-stand-gui-fix: true
|
||||
#Automatically put lapis lazuli in enchanting tables for clients lower than 1.8
|
||||
enchanting-gui-fix: true
|
||||
#fix the lily pad bounding box to prevent clients lower than 1.9 from glitching
|
||||
#Fix the lily pad bounding box to prevent clients lower than 1.9 from glitching
|
||||
lily-pad-fix: true
|
||||
#If set to true, this plugin will play sounds for 1.8 and lower that got clientside on 1.9 and higher
|
||||
sound-fix: true
|
||||
#Inform your players that they are using an outdated minecraft version
|
||||
#max-version specifies the maximum protocol version of players being notified. See http://wiki.vg/Protocol_version_numbers for protocol version numbers
|
||||
#interval can be set to JOIN to notify players when joining the server. If you set it to any integer number it will broadcast the message every x ticks.
|
||||
|
Loading…
Reference in New Issue
Block a user