PS bug fixes, more argument readers, NMS utils

This commit is contained in:
Olof Larsson 2012-12-17 13:13:12 +01:00
parent 79c42a56e6
commit 73ecbd659a
12 changed files with 428 additions and 6 deletions

View File

@ -155,8 +155,8 @@ public class PS implements Cloneable
// Field: pitch // Field: pitch
@SerializedName("p") @SerializedName("p")
@Getter protected Float pitch = null; @Getter @Setter protected Float pitch = null;
public void setPitch(Float val) /*public void setPitch(Float val)
{ {
if (val == null) if (val == null)
{ {
@ -166,7 +166,7 @@ public class PS implements Cloneable
{ {
this.pitch = (val + 360F) % 360F; this.pitch = (val + 360F) % 360F;
} }
} }*/
// Field: yaw // Field: yaw
@SerializedName("y") @SerializedName("y")
@ -233,7 +233,7 @@ public class PS implements Cloneable
Float yaw = this.getYaw(); Float yaw = this.getYaw();
if (yaw == null) yaw = 0F; 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() public synchronized Block getBlock()

View File

@ -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<ChatColor>
{
@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<String> altNames(MCommand mcommand)
{
List<String> ret = new ArrayList<String>();
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; }
}

View File

@ -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<Difficulty>
{
@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<String> altNames(MCommand mcommand)
{
return MUtil.list("peaceful", "easy", "normal", "hard");
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ARDifficulty i = new ARDifficulty();
public static ARDifficulty get() { return i; }
}

View File

@ -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<GameMode>
{
@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<String> altNames(MCommand mcommand)
{
return MUtil.list("survival", "creative", "adventure");
}
// -------------------------------------------- //
// INSTANCE
// -------------------------------------------- //
private static ARGameMode i = new ARGameMode();
public static ARGameMode get() { return i; }
}

View File

@ -4,6 +4,7 @@ import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -30,6 +31,13 @@ public class MCoreAfterPlayerTeleportEvent extends Event implements Runnable
public Player getPlayer() { return this.bukkitEvent.getPlayer(); } public Player getPlayer() { return this.bukkitEvent.getPlayer(); }
public TeleportCause getCause() { return this.bukkitEvent.getCause(); } 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 // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -19,9 +19,9 @@ public class CmdUsysMultiverseNew extends UsysCommand
{ {
String id = this.arg(0); String id = this.arg(0);
if (MultiverseColl.i.getIds().contains(id)) if (MultiverseColl.i.containsId(id))
{ {
msg("<b>The multiverse <h>%s<b> alread exists.", id); msg("<b>The multiverse <h>%s<b> already exists.", id);
return; return;
} }

View File

@ -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<Integer, String> getBiomeIdNames()
{
Map<Integer, String> ret = new LinkedHashMap<Integer, String>();
for(BiomeBase bb : BiomeBase.biomes)
{
if (bb == null) continue;
ret.put(bb.id, bb.y);
}
return ret;
}
public static Entry<Integer, String> 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<Integer, String>(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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -178,6 +178,25 @@ public class MUtil
return new ArrayList<T>(coll).get(index); return new ArrayList<T>(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 // SORTING
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -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);
}
}

View File

@ -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();
}
}