diff --git a/src/com/massivecraft/mcore2/MPlugin.java b/src/com/massivecraft/mcore2/MPlugin.java index d8239751..ea118eeb 100644 --- a/src/com/massivecraft/mcore2/MPlugin.java +++ b/src/com/massivecraft/mcore2/MPlugin.java @@ -4,6 +4,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import com.massivecraft.mcore2.cmd.Cmd; @@ -14,8 +15,7 @@ import com.massivecraft.mcore2.persist.Persist; import com.massivecraft.mcore2.util.LibLoader; import com.massivecraft.mcore2.util.Txt; - -public abstract class MPlugin extends JavaPlugin +public abstract class MPlugin extends JavaPlugin implements Listener { // Tools public Cmd cmd; diff --git a/src/com/massivecraft/mcore2/cmd/req/ReqHasPerm.java b/src/com/massivecraft/mcore2/cmd/req/ReqHasPerm.java index 728d72dd..0454b45d 100644 --- a/src/com/massivecraft/mcore2/cmd/req/ReqHasPerm.java +++ b/src/com/massivecraft/mcore2/cmd/req/ReqHasPerm.java @@ -27,5 +27,10 @@ public class ReqHasPerm implements IReq { return Perm.getForbiddenMessage(this.perm); } + + public static ReqHasPerm get(String perm) + { + return new ReqHasPerm(perm); + } } diff --git a/src/com/massivecraft/mcore2/cmd/req/ReqIsPlayer.java b/src/com/massivecraft/mcore2/cmd/req/ReqIsPlayer.java index 48e1d913..07e93878 100644 --- a/src/com/massivecraft/mcore2/cmd/req/ReqIsPlayer.java +++ b/src/com/massivecraft/mcore2/cmd/req/ReqIsPlayer.java @@ -25,4 +25,9 @@ public class ReqIsPlayer implements IReq { return instance; } + + public static ReqIsPlayer get() + { + return instance; + } } diff --git a/src/com/massivecraft/mcore2/util/MUtil.java b/src/com/massivecraft/mcore2/util/MUtil.java new file mode 100644 index 00000000..bdc6836a --- /dev/null +++ b/src/com/massivecraft/mcore2/util/MUtil.java @@ -0,0 +1,174 @@ +package com.massivecraft.mcore2.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Projectile; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.event.player.PlayerInteractEvent; + +import com.massivecraft.mcore2.MCore; + +public class MUtil +{ + // -------------------------------------------- // + // MATERIAL FACTS + // -------------------------------------------- // + + public final static Set foodMaterials = new HashSet(MUtil.list( + Material.APPLE, + Material.BREAD, + Material.COOKED_BEEF, + Material.COOKED_CHICKEN, + Material.COOKED_FISH, + Material.COOKIE, + Material.GRILLED_PORK, + Material.GOLDEN_APPLE, + Material.MELON, + Material.MUSHROOM_SOUP, + Material.PORK, + Material.RAW_BEEF, + Material.RAW_CHICKEN, + Material.RAW_FISH, + Material.ROTTEN_FLESH, + Material.SPIDER_EYE + )); + + // -------------------------------------------- // + // EVENT DERP + // -------------------------------------------- // + + public static Material getEatenMaterial(PlayerInteractEvent event) + { + Action action = event.getAction(); + if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) return null; + + Material ret = null; + + if (action == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.CAKE_BLOCK) + { + ret = Material.CAKE_BLOCK; + } + else if (foodMaterials.contains(event.getMaterial())) + { + ret = event.getMaterial(); + } + + return ret; + } + + public static boolean isCombatEvent(EntityDamageEvent event) + { + if (event.getCause() != DamageCause.ENTITY_ATTACK && event.getCause() != DamageCause.PROJECTILE) return false; + return event instanceof EntityDamageByEntityEvent; + } + + public static boolean isCloseCombatEvent(EntityDamageEvent event) + { + if (event.getCause() != DamageCause.ENTITY_ATTACK) return false; + return event instanceof EntityDamageByEntityEvent; + } + + public static Entity getLiableDamager(EntityDamageEvent event) + { + if ( ! (event instanceof EntityDamageByEntityEvent)) return null; + EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent)event; + Entity ret = edbeEvent.getDamager(); + if (ret instanceof Projectile) + { + ret = ((Projectile)ret).getShooter(); + } + return ret; + } + + // -------------------------------------------- // + // SIMPLE CONSTRUCTORS + // -------------------------------------------- // + + public static List list(T... items) + { + return new ArrayList(Arrays.asList(items)); + } + + public static Set set(T... items) + { + return new LinkedHashSet(Arrays.asList(items)); + } + + @SuppressWarnings("unchecked") + public static Map map(K key1, V value1, Object... objects) + { + Map ret = new LinkedHashMap(); + + ret.put(key1, value1); + + Iterator iter = Arrays.asList(objects).iterator(); + while (iter.hasNext()) + { + K key = (K) iter.next(); + V value = (V) iter.next(); + ret.put(key, value); + } + + return ret; + } + + // -------------------------------------------- // + // SORTING + // -------------------------------------------- // + + //http://stackoverflow.com/questions/2864840/treemap-sort-by-value + public static > SortedSet> entriesSortedByValues(Map map) { + SortedSet> sortedEntries = new TreeSet>( + new Comparator>() { + @Override public int compare(Map.Entry e1, Map.Entry e2) { + int res = e1.getValue().compareTo(e2.getValue()); + return res != 0 ? res : 1; // Special fix to preserve items with equal values + } + } + ); + sortedEntries.addAll(map.entrySet()); + return sortedEntries; + } + + // -------------------------------------------- // + // MATH + // -------------------------------------------- // + public static T limitNumber(T d, T min, T max) + { + if (min instanceof Number && d.doubleValue() < min.doubleValue()) + { + return min; + } + + if (max instanceof Number && d.doubleValue() > max.doubleValue()) + { + return max; + } + + return d; + } + + public static long probabilityRound(double val) + { + long ret = (long) Math.floor(val); + double prob = val % 1; + if (MCore.random.nextDouble() < prob) ret += 1; + return ret; + } +} diff --git a/src/com/massivecraft/mcore2/util/PlayerUtil.java b/src/com/massivecraft/mcore2/util/PlayerUtil.java index 75a3229f..7887914a 100644 --- a/src/com/massivecraft/mcore2/util/PlayerUtil.java +++ b/src/com/massivecraft/mcore2/util/PlayerUtil.java @@ -4,7 +4,12 @@ import java.io.File; import java.util.Set; import java.util.TreeSet; +import net.minecraft.server.EntityPlayer; +import net.minecraft.server.Packet8UpdateHealth; + import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; public class PlayerUtil { @@ -23,4 +28,11 @@ public class PlayerUtil allVisitorNames.add(playername); } } + + public static void sendHealthFoodUpdatePacket(Player player) + { + CraftPlayer cplayer = (CraftPlayer)player; + EntityPlayer eplayer = cplayer.getHandle(); + eplayer.netServerHandler.sendPacket(new Packet8UpdateHealth(eplayer.getHealth(), eplayer.getFoodData().a(), eplayer.getFoodData().c())); + } } diff --git a/src/com/massivecraft/mcore2/util/Txt.java b/src/com/massivecraft/mcore2/util/Txt.java index 4ba63d25..d53e9f81 100644 --- a/src/com/massivecraft/mcore2/util/Txt.java +++ b/src/com/massivecraft/mcore2/util/Txt.java @@ -61,6 +61,16 @@ public class Txt parseReplacements.put("", "\u00A7d"); parseReplacements.put("", "\u00A7e"); parseReplacements.put("", "\u00A7f"); + parseReplacements.put("", "\u00A7k"); + parseReplacements.put("", "\u00A7l"); + parseReplacements.put("", "\u00A7l"); + parseReplacements.put("", "\u00A7m"); + parseReplacements.put("", "\u00A7m"); + parseReplacements.put("", "\u00A7n"); + parseReplacements.put("", "\u00A7n"); + parseReplacements.put("", "\u00A7o"); + parseReplacements.put("", "\u00A7o"); + parseReplacements.put("", "\u00A7r"); // Color by semantic functionality parseReplacements.put("", "\u00A72"); @@ -157,7 +167,7 @@ public class Txt // -------------------------------------------- // public static ArrayList wrap(final String string) { - return new ArrayList(Arrays.asList(TextWrapper.wrapText(string))); + return wrap(Arrays.asList(string.split("\\r?\\n"))); } public static ArrayList wrap(final Collection strings) @@ -165,7 +175,7 @@ public class Txt ArrayList ret = new ArrayList(); for (String line : strings) { - ret.addAll(wrap(line)); + ret.addAll(Arrays.asList(TextWrapper.wrapText(line))); } return ret; }