|
| 1 | +package me.dags.daflightmanager.bukkit; |
| 2 | + |
| 3 | +import org.bukkit.Bukkit; |
| 4 | +import org.bukkit.configuration.Configuration; |
| 5 | +import org.bukkit.entity.Player; |
| 6 | +import org.bukkit.plugin.java.JavaPlugin; |
| 7 | +import org.bukkit.plugin.messaging.PluginMessageListener; |
| 8 | + |
| 9 | +import java.nio.ByteBuffer; |
| 10 | + |
| 11 | +public class DFMBukkit extends JavaPlugin implements PluginMessageListener |
| 12 | +{ |
| 13 | + private Config config = Config.defaultConfig(); |
| 14 | + |
| 15 | + @Override |
| 16 | + public void onEnable() |
| 17 | + { |
| 18 | + Bukkit.getMessenger().registerIncomingPluginChannel(this, "DAFLIGHT-CONNECT", this); |
| 19 | + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "DAFLIGHT-FLY"); |
| 20 | + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "DAFLIGHT-SPRINT"); |
| 21 | + config = loadConfig(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void onPluginMessageReceived(String s, Player player, byte[] bytes) |
| 26 | + { |
| 27 | + getLogger().info(String.format("DaFlight connect message received from user %s", player.getName())); |
| 28 | + |
| 29 | + Float fly = config.getMaxFlySpeed(player); |
| 30 | + Float sprint = config.getMaxSprintSpeed(player); |
| 31 | + |
| 32 | + byte[] flyData = ByteBuffer.allocate(4).putFloat(fly).array(); |
| 33 | + byte[] sprintData = ByteBuffer.allocate(4).putFloat(sprint).array(); |
| 34 | + |
| 35 | + player.sendPluginMessage(this, "DAFLIGHT-FLY", flyData); |
| 36 | + player.sendPluginMessage(this, "DAFLIGHT-SPRINT", sprintData); |
| 37 | + } |
| 38 | + |
| 39 | + private Config loadConfig() |
| 40 | + { |
| 41 | + Configuration configuration = getConfig(); |
| 42 | + if (configuration.contains("flySpeeds") && configuration.contains("sprintSpeeds")) |
| 43 | + { |
| 44 | + Config config = new Config(); |
| 45 | + configuration.getConfigurationSection("flySpeeds").getValues(false).entrySet().forEach(e -> { |
| 46 | + config.flySpeeds.put(e.getKey(), ((Double) e.getValue()).floatValue()); |
| 47 | + }); |
| 48 | + configuration.getConfigurationSection("sprintSpeeds").getValues(false).entrySet().forEach(e -> { |
| 49 | + config.flySpeeds.put(e.getKey(), ((Double) e.getValue()).floatValue()); |
| 50 | + }); |
| 51 | + return config; |
| 52 | + } |
| 53 | + configuration.set("flySpeeds", config.flySpeeds); |
| 54 | + configuration.set("sprintSpeeds", config.sprintSpeeds); |
| 55 | + saveConfig(); |
| 56 | + return config; |
| 57 | + } |
| 58 | +} |
0 commit comments