From 73ecbd659ae208672f741ef5226e79d2e0395c2a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 17 Dec 2012 13:13:12 +0100 Subject: [PATCH] PS bug fixes, more argument readers, NMS utils --- src/com/massivecraft/mcore5/PS.java | 8 +- .../mcore5/cmd/arg/ARChatColor.java | 73 ++++++++++++++++++ .../mcore5/cmd/arg/ARDifficulty.java | 58 +++++++++++++++ .../mcore5/cmd/arg/ARGameMode.java | 54 ++++++++++++++ .../event/MCoreAfterPlayerTeleportEvent.java | 8 ++ .../mcore5/usys/cmd/CmdUsysMultiverseNew.java | 4 +- .../massivecraft/mcore5/util/BiomeUtil.java | 65 ++++++++++++++++ .../mcore5/util/InventoryUtil.java | 28 +++++++ .../massivecraft/mcore5/util/LightUtil.java | 28 +++++++ src/com/massivecraft/mcore5/util/MUtil.java | 19 +++++ .../mcore5/util/PotionPaketUtil.java | 74 +++++++++++++++++++ .../mcore5/util/ThrownPotionUtil.java | 15 ++++ 12 files changed, 428 insertions(+), 6 deletions(-) create mode 100644 src/com/massivecraft/mcore5/cmd/arg/ARChatColor.java create mode 100644 src/com/massivecraft/mcore5/cmd/arg/ARDifficulty.java create mode 100644 src/com/massivecraft/mcore5/cmd/arg/ARGameMode.java create mode 100644 src/com/massivecraft/mcore5/util/BiomeUtil.java create mode 100644 src/com/massivecraft/mcore5/util/InventoryUtil.java create mode 100644 src/com/massivecraft/mcore5/util/LightUtil.java create mode 100644 src/com/massivecraft/mcore5/util/PotionPaketUtil.java create mode 100644 src/com/massivecraft/mcore5/util/ThrownPotionUtil.java diff --git a/src/com/massivecraft/mcore5/PS.java b/src/com/massivecraft/mcore5/PS.java index 5e025aa1..208e351c 100644 --- a/src/com/massivecraft/mcore5/PS.java +++ b/src/com/massivecraft/mcore5/PS.java @@ -155,8 +155,8 @@ public class PS implements Cloneable // Field: pitch @SerializedName("p") - @Getter protected Float pitch = null; - public void setPitch(Float val) + @Getter @Setter protected Float pitch = null; + /*public void setPitch(Float val) { if (val == null) { @@ -166,7 +166,7 @@ public class PS implements Cloneable { this.pitch = (val + 360F) % 360F; } - } + }*/ // Field: yaw @SerializedName("y") @@ -233,7 +233,7 @@ public class PS implements Cloneable Float yaw = this.getYaw(); if (yaw == null) yaw = 0F; - return new Location(world, x, y, z, pitch, yaw); + return new Location(world, x, y, z, yaw, pitch); } public synchronized Block getBlock() diff --git a/src/com/massivecraft/mcore5/cmd/arg/ARChatColor.java b/src/com/massivecraft/mcore5/cmd/arg/ARChatColor.java new file mode 100644 index 00000000..a8dcfb76 --- /dev/null +++ b/src/com/massivecraft/mcore5/cmd/arg/ARChatColor.java @@ -0,0 +1,73 @@ +package com.massivecraft.mcore5.cmd.arg; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.bukkit.ChatColor; + +import com.massivecraft.mcore5.cmd.MCommand; + +public class ARChatColor extends ARAbstractSelect +{ + @Override + public String typename() + { + return "chat color"; + } + + @Override + public ChatColor select(String str, MCommand mcommand) + { + ChatColor ret = null; + + str = getToCompare(str); + + for (ChatColor cc : ChatColor.values()) + { + + + String ccstr = getToCompare(cc.name()); + if ( ! ccstr.equals(str)) continue; + ret = cc; + break; + } + + return ret; + } + + @Override + public Collection altNames(MCommand mcommand) + { + List ret = new ArrayList(); + + for (ChatColor cc : ChatColor.values()) + { + ret.add(cc.toString()+getToCompare(cc.name())); + } + + return ret; + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + // "DARK_RED" --> "darkred" + // "DARK RED" --> "darkred" + public static String getToCompare(String str) + { + str = str.toLowerCase(); + str = str.replace("_", ""); + str = str.replace(" ", ""); + return str; + } + + // -------------------------------------------- // + // INSTANCE + // -------------------------------------------- // + + private static ARChatColor i = new ARChatColor(); + public static ARChatColor get() { return i; } + +} diff --git a/src/com/massivecraft/mcore5/cmd/arg/ARDifficulty.java b/src/com/massivecraft/mcore5/cmd/arg/ARDifficulty.java new file mode 100644 index 00000000..c680cd57 --- /dev/null +++ b/src/com/massivecraft/mcore5/cmd/arg/ARDifficulty.java @@ -0,0 +1,58 @@ +package com.massivecraft.mcore5.cmd.arg; + +import java.util.Collection; + +import org.bukkit.Difficulty; + +import com.massivecraft.mcore5.cmd.MCommand; +import com.massivecraft.mcore5.util.MUtil; + +public class ARDifficulty extends ARAbstractSelect +{ + @Override + public String typename() + { + return "difficulty"; + } + + @Override + public Difficulty select(String str, MCommand mcommand) + { + Difficulty ret = null; + + str = str.toLowerCase(); + + if (str.startsWith("p")) + { + ret = Difficulty.PEACEFUL; + } + else if (str.startsWith("e")) + { + ret = Difficulty.EASY; + } + else if (str.startsWith("n")) + { + ret = Difficulty.NORMAL; + } + else if (str.startsWith("h")) + { + ret = Difficulty.HARD; + } + + return ret; + } + + @Override + public Collection altNames(MCommand mcommand) + { + return MUtil.list("peaceful", "easy", "normal", "hard"); + } + + // -------------------------------------------- // + // INSTANCE + // -------------------------------------------- // + + private static ARDifficulty i = new ARDifficulty(); + public static ARDifficulty get() { return i; } + +} diff --git a/src/com/massivecraft/mcore5/cmd/arg/ARGameMode.java b/src/com/massivecraft/mcore5/cmd/arg/ARGameMode.java new file mode 100644 index 00000000..2b0cfd31 --- /dev/null +++ b/src/com/massivecraft/mcore5/cmd/arg/ARGameMode.java @@ -0,0 +1,54 @@ +package com.massivecraft.mcore5.cmd.arg; + +import java.util.Collection; + +import org.bukkit.GameMode; + +import com.massivecraft.mcore5.cmd.MCommand; +import com.massivecraft.mcore5.util.MUtil; + +public class ARGameMode extends ARAbstractSelect +{ + @Override + public String typename() + { + return "game mode"; + } + + @Override + public GameMode select(String str, MCommand mcommand) + { + GameMode ret = null; + + str = str.toLowerCase(); + + if (str.startsWith("s")) + { + ret = GameMode.SURVIVAL; + } + else if (str.startsWith("c")) + { + ret = GameMode.CREATIVE; + } + else if (str.startsWith("a")) + { + ret = GameMode.ADVENTURE; + } + + return ret; + } + + @Override + public Collection altNames(MCommand mcommand) + { + return MUtil.list("survival", "creative", "adventure"); + } + + // -------------------------------------------- // + // INSTANCE + // -------------------------------------------- // + + private static ARGameMode i = new ARGameMode(); + public static ARGameMode get() { return i; } + +} diff --git a/src/com/massivecraft/mcore5/event/MCoreAfterPlayerTeleportEvent.java b/src/com/massivecraft/mcore5/event/MCoreAfterPlayerTeleportEvent.java index 79cbd10d..f5c01734 100644 --- a/src/com/massivecraft/mcore5/event/MCoreAfterPlayerTeleportEvent.java +++ b/src/com/massivecraft/mcore5/event/MCoreAfterPlayerTeleportEvent.java @@ -4,6 +4,7 @@ import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; @@ -30,6 +31,13 @@ public class MCoreAfterPlayerTeleportEvent extends Event implements Runnable public Player getPlayer() { return this.bukkitEvent.getPlayer(); } public TeleportCause getCause() { return this.bukkitEvent.getCause(); } + public boolean isCrossWorlds() + { + World worldFrom = this.getFrom().getWorld(); + World worldTo = this.getTo().getWorld(); + return ! worldFrom.equals(worldTo); + } + // -------------------------------------------- // // CONSTRUCT // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore5/usys/cmd/CmdUsysMultiverseNew.java b/src/com/massivecraft/mcore5/usys/cmd/CmdUsysMultiverseNew.java index dd6c8641..09c36f62 100644 --- a/src/com/massivecraft/mcore5/usys/cmd/CmdUsysMultiverseNew.java +++ b/src/com/massivecraft/mcore5/usys/cmd/CmdUsysMultiverseNew.java @@ -19,9 +19,9 @@ public class CmdUsysMultiverseNew extends UsysCommand { String id = this.arg(0); - if (MultiverseColl.i.getIds().contains(id)) + if (MultiverseColl.i.containsId(id)) { - msg("The multiverse %s alread exists.", id); + msg("The multiverse %s already exists.", id); return; } diff --git a/src/com/massivecraft/mcore5/util/BiomeUtil.java b/src/com/massivecraft/mcore5/util/BiomeUtil.java new file mode 100644 index 00000000..0272c4ba --- /dev/null +++ b/src/com/massivecraft/mcore5/util/BiomeUtil.java @@ -0,0 +1,65 @@ +package com.massivecraft.mcore5.util; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.AbstractMap.SimpleEntry; + +import org.bukkit.World; +import org.bukkit.craftbukkit.CraftWorld; + +import net.minecraft.server.BiomeBase; +import net.minecraft.server.WorldServer; + +public class BiomeUtil +{ + public static Map getBiomeIdNames() + { + Map ret = new LinkedHashMap(); + for(BiomeBase bb : BiomeBase.biomes) + { + if (bb == null) continue; + ret.put(bb.id, bb.y); + } + return ret; + } + + public static Entry getBiomeIdAndNameAt(World world, int x, int z) + { + CraftWorld craftWorld = (CraftWorld)world; + WorldServer worldServer = craftWorld.getHandle(); + + BiomeBase biomeBase = worldServer.getBiome(x, z); + + Integer id = biomeBase.id; + String name = biomeBase.y; + + return new SimpleEntry(id, name); + } + + public static void setBiomeIdAt(World world, int x, int z, int id) + { + CraftWorld craftWorld = (CraftWorld)world; + WorldServer worldServer = craftWorld.getHandle(); + + BiomeBase bb = BiomeBase.biomes[id]; + if (craftWorld.loadChunk(x >> 4, z >> 4, false)) { + net.minecraft.server.Chunk chunk = worldServer.getChunkAtWorldCoords(x, z); + + if (chunk != null) { + byte[] biomevals = chunk.m(); + biomevals[((z & 0xF) << 4) | (x & 0xF)] = (byte)bb.id; + } + } + } + + public static int getBiomeIdAt(World world, int x, int z) + { + return getBiomeIdAndNameAt(world, x, z).getKey(); + } + + public static String getBiomeNameAt(World world, int x, int z) + { + return getBiomeIdAndNameAt(world, x, z).getValue(); + } +} diff --git a/src/com/massivecraft/mcore5/util/InventoryUtil.java b/src/com/massivecraft/mcore5/util/InventoryUtil.java new file mode 100644 index 00000000..099dd8d1 --- /dev/null +++ b/src/com/massivecraft/mcore5/util/InventoryUtil.java @@ -0,0 +1,28 @@ +package com.massivecraft.mcore5.util; + +import org.bukkit.craftbukkit.inventory.CraftInventoryCustom; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +public class InventoryUtil +{ + public static Inventory create(int size) + { + return create(null, size, "Chest"); + } + + public static Inventory create(InventoryHolder owner, int size) + { + return create(owner, size, "Chest"); + } + + public static Inventory create(int size, String title) + { + return create(null, size, title); + } + + public static Inventory create(InventoryHolder owner, int size, String title) + { + return new CraftInventoryCustom(owner, size, title); + } +} diff --git a/src/com/massivecraft/mcore5/util/LightUtil.java b/src/com/massivecraft/mcore5/util/LightUtil.java new file mode 100644 index 00000000..a167372b --- /dev/null +++ b/src/com/massivecraft/mcore5/util/LightUtil.java @@ -0,0 +1,28 @@ +package com.massivecraft.mcore5.util; + +import net.minecraft.server.WorldServer; + +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.craftbukkit.CraftWorld; + +public class LightUtil +{ + // -------------------------------------------- // + // RECALC LIGHT LEVEL AT + // -------------------------------------------- // + // This method will update the light level for the block. + // It will however only work properly if all chunks that are around the chunk the block is in are loaded. + + public static void recalcLightLevelAt(Block block) + { + recalcLightLevelAt(block.getWorld(), block.getX(), block.getY(), block.getZ()); + } + + public static void recalcLightLevelAt(World world, int x, int y, int z) + { + CraftWorld cworld = (CraftWorld)world; + WorldServer worldServer = cworld.getHandle(); + worldServer.z(x, y, z); + } +} diff --git a/src/com/massivecraft/mcore5/util/MUtil.java b/src/com/massivecraft/mcore5/util/MUtil.java index c1a2c058..bb62689f 100644 --- a/src/com/massivecraft/mcore5/util/MUtil.java +++ b/src/com/massivecraft/mcore5/util/MUtil.java @@ -178,6 +178,25 @@ public class MUtil return new ArrayList(coll).get(index); } + // -------------------------------------------- // + // LE NICE EQUALS + // -------------------------------------------- // + + public static boolean equals(Object herp, Object derp) + { + if (herp == null && derp == null) + { + return true; + } + + if (herp == null || derp == null) + { + return false; + } + + return herp.equals(derp); + } + // -------------------------------------------- // // SORTING // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore5/util/PotionPaketUtil.java b/src/com/massivecraft/mcore5/util/PotionPaketUtil.java new file mode 100644 index 00000000..c4ee96d6 --- /dev/null +++ b/src/com/massivecraft/mcore5/util/PotionPaketUtil.java @@ -0,0 +1,74 @@ +package com.massivecraft.mcore5.util; + +import net.minecraft.server.Packet41MobEffect; +import net.minecraft.server.Packet42RemoveMobEffect; + +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +public class PotionPaketUtil +{ + // -------------------------------------------- // + // ASSUMING THE PLAYER SELF + // -------------------------------------------- // + + public static void add(Player player, PotionEffectType potionEffectType, int amplifier, int duration) + { + add(player, player, potionEffectType, amplifier, duration); + } + + public static void add(Player player, PotionEffect potionEffect) + { + add(player, player, potionEffect); + } + + public static void remove(Player player, PotionEffectType potionEffectType) + { + remove(player, player, potionEffectType); + } + + // -------------------------------------------- // + // USING BUKKIT STUFF + // -------------------------------------------- // + + public static void add(Player player, Entity entity, PotionEffectType potionEffectType, int amplifier, int duration) + { + add(player, entity.getEntityId(), (byte)potionEffectType.getId(), (byte)amplifier, (short)duration); + } + + public static void add(Player player, Entity entity, PotionEffect potionEffect) + { + add(player, entity.getEntityId(), (byte)potionEffect.getType().getId(), (byte)potionEffect.getAmplifier(), (short)potionEffect.getDuration()); + } + + public static void remove(Player player, Entity entity, PotionEffectType potionEffectType) + { + remove(player, entity.getEntityId(), (byte)potionEffectType.getId()); + } + + // -------------------------------------------- // + // CORE + // -------------------------------------------- // + + public static void add(Player player, int entityId, byte effectId, byte amplifier, short duration) + { + Packet41MobEffect pm = new Packet41MobEffect(); + pm.a = entityId; + pm.b = effectId; + pm.c = amplifier; + pm.d = duration; + ((CraftPlayer)player).getHandle().netServerHandler.sendPacket(pm); + } + + public static void remove(Player player, int entityId, byte effectId) + { + Packet42RemoveMobEffect pr = new Packet42RemoveMobEffect(); + pr.a = entityId; + pr.b = effectId; + ((CraftPlayer)player).getHandle().netServerHandler.sendPacket(pr); + } + +} diff --git a/src/com/massivecraft/mcore5/util/ThrownPotionUtil.java b/src/com/massivecraft/mcore5/util/ThrownPotionUtil.java new file mode 100644 index 00000000..a8bc3a5b --- /dev/null +++ b/src/com/massivecraft/mcore5/util/ThrownPotionUtil.java @@ -0,0 +1,15 @@ +package com.massivecraft.mcore5.util; + +import org.bukkit.craftbukkit.entity.CraftThrownPotion; +import org.bukkit.entity.ThrownPotion; + +// PR to add this feature to the API: +// https://github.com/Bukkit/Bukkit/pull/737 +public class ThrownPotionUtil +{ + public static int getPotionValue(ThrownPotion potion) + { + CraftThrownPotion cpotion = (CraftThrownPotion)potion; + return cpotion.getHandle().getPotionValue(); + } +}