Skip to content

Commit

Permalink
Move disguise saddle methods from Util to new class SpecialSaddles.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed Sep 26, 2018
1 parent 6ed0950 commit 8088c31
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 145 deletions.
12 changes: 6 additions & 6 deletions src/nu/nerd/easyrider/EasyRider.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void onDisable() {
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
_state.put(player.getName(), new PlayerState(player, _playerConfig));
Util.refreshSaddleDisguises();
SpecialSaddles.refreshSaddleDisguises();
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -683,11 +683,11 @@ public void onInventoryClick(InventoryClickEvent event) {
ItemStack newSaddle = event.getInventory().getItem(0);
if ((oldSaddle == null && newSaddle != null) ||
(oldSaddle != null && !oldSaddle.equals(newSaddle))) {
EntityType disguiseEntityType = Util.getSaddleDisguiseType(abstractHorse);
EntityType disguiseEntityType = SpecialSaddles.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType == null) {
getDisguiseProvider().removeDisguise(abstractHorse);
} else {
Util.applySaddleDisguise(abstractHorse, rider, disguiseEntityType, false, true);
SpecialSaddles.applySaddleDisguise(abstractHorse, rider, disguiseEntityType, false, true);
}
}
}, 1);
Expand Down Expand Up @@ -758,9 +758,9 @@ public void onVehicleEnter(VehicleEnterEvent event) {
Util.entityTypeName(abstractHorse) + ".");
}

EntityType disguiseEntityType = Util.getSaddleDisguiseType(abstractHorse);
EntityType disguiseEntityType = SpecialSaddles.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType != null) {
Util.applySaddleDisguise(abstractHorse, player, disguiseEntityType, false, true);
SpecialSaddles.applySaddleDisguise(abstractHorse, player, disguiseEntityType, false, true);
}

if (CONFIG.DEBUG_EVENTS && savedHorse.isDebug()) {
Expand Down Expand Up @@ -793,7 +793,7 @@ public void onVehicleExit(VehicleExitEvent event) {

// Clear disguise on dismount.
if (getDisguiseProvider() != null) {
EntityType disguiseEntityType = Util.getSaddleDisguiseType(abstractHorse);
EntityType disguiseEntityType = SpecialSaddles.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType != null) {
getDisguiseProvider().removeDisguise(abstractHorse);
}
Expand Down
149 changes: 149 additions & 0 deletions src/nu/nerd/easyrider/SpecialSaddles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package nu.nerd.easyrider;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Llama;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.FixedMetadataValue;

// ----------------------------------------------------------------------------
/**
* Namespace class for holding utility methods pertaining to special saddles.
*/
public class SpecialSaddles {
// ------------------------------------------------------------------------
/**
* Get the ItemStack in the saddle slot of a horse, donkey or mule.
*
* @param abstractHorse the horse-like entity (includes llamas).
* @return the ItemStack in the saddle slot, or null if there is no saddle
* slot (as in the case of llamas).
*/
public static ItemStack getSaddleItemStack(AbstractHorse abstractHorse) {
if (abstractHorse instanceof Llama) {
return null;
}

// Mules, donkeys, zombie- and skeletal- horses do not have a
// HorseInventory. So get the saddle by ID rather than calling
// HorseInventory.getSaddle().
return abstractHorse.getInventory().getItem(0);
}

// ------------------------------------------------------------------------
/**
* Re-apply disguises to all disguised steeds when a player joins.
*/
public static void refreshSaddleDisguises() {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.getVehicle() instanceof AbstractHorse) {
AbstractHorse abstractHorse = (AbstractHorse) onlinePlayer.getVehicle();
EntityType disguiseEntityType = SpecialSaddles.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType != null) {
boolean showToRider = SpecialSaddles.isSaddleDisguiseVisibleToRider(abstractHorse);
SpecialSaddles.applySaddleDisguise(abstractHorse, onlinePlayer, disguiseEntityType, showToRider, false);
}
}
}
}

// ------------------------------------------------------------------------
/**
* Disguise a horse as a specified EntityType and notify a player when it is
* still disguised.
*
* @param abstractHorse the horse-like entity.
* @param rider the rider, to be notified if a disguise is applied.
* @param disguiseEntityType the EntityType of the disguise.
* @param showToRider if true, the disguise is visible to the rider.
* @param tellRider if true, tell the rider what disguise is in use.
*/
public static void applySaddleDisguise(AbstractHorse abstractHorse, Player rider, EntityType disguiseEntityType,
boolean showToRider, boolean tellRider) {
if (disguiseEntityType == null || EasyRider.PLUGIN.getDisguiseProvider() == null) {
return;
}

Set<Player> players = new HashSet<>(Bukkit.getOnlinePlayers());
if (!showToRider) {
players.remove(rider);
}
boolean validDisguise = EasyRider.PLUGIN.getDisguiseProvider().applyDisguise(abstractHorse, disguiseEntityType, players);
if (validDisguise) {
if (tellRider) {
rider.sendMessage(ChatColor.GOLD + "Your steed is disguised as " + disguiseEntityType + "!");
}

abstractHorse.removeMetadata(SpecialSaddles.SELF_DISGUISE_KEY, EasyRider.PLUGIN);
if (showToRider) {
abstractHorse.setMetadata(SpecialSaddles.SELF_DISGUISE_KEY, new FixedMetadataValue(EasyRider.PLUGIN, null));
}
} else {
Logger logger = EasyRider.PLUGIN.getLogger();
logger.warning("Horse " + abstractHorse.getUniqueId() + " accessed by " + rider.getName() +
" has a saddle with unsupported disguise " + disguiseEntityType + ".");
}
}

// ------------------------------------------------------------------------
/**
* Return true if the saddle disguise of the specified horse is visible to
* the rider.
*
* @param abstractHorse the horse-like entity.
* @return true if the saddle disguise of the specified horse is visible to
* the rider.
*/
public static boolean isSaddleDisguiseVisibleToRider(AbstractHorse abstractHorse) {
return !abstractHorse.getMetadata(SpecialSaddles.SELF_DISGUISE_KEY).isEmpty();
}

// ------------------------------------------------------------------------
/**
* Return the EntityType of the disguise associated with a horse's saddle,
* or null if the saddle doesn't confer a disguise (or it's not a saddle).
*
* @param abstractHorse the horse-like entity.
* @return the EntityType of the disguise, or null if no disguise should be
* applied.
*/
public static EntityType getSaddleDisguiseType(AbstractHorse abstractHorse) {
ItemStack saddle = SpecialSaddles.getSaddleItemStack(abstractHorse);
if (saddle == null || saddle.getType() != Material.SADDLE) {
return null;
}

ItemMeta meta = saddle.getItemMeta();
if (meta != null && meta.hasLore()) {
for (String lore : meta.getLore()) {
if (lore.startsWith(EasyRider.DISGUISE_PREFIX)) {
String entityTypeName = lore.substring(EasyRider.DISGUISE_PREFIX.length()).trim().toUpperCase();
try {
return EntityType.valueOf(entityTypeName);
} catch (IllegalArgumentException ex) {
}
}
}
}
return null;
}

// ------------------------------------------------------------------------
/**
* Metadata key for metadata signifying that a saddle disguise is visible to
* the rider.
*
* If metadata with this key is absent, the rider cannot see the disguise.
*/
static final String SELF_DISGUISE_KEY = "EasyRider_self_disguise";

} // class SpecialSaddles
135 changes: 0 additions & 135 deletions src/nu/nerd/easyrider/Util.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package nu.nerd.easyrider;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Llama;
import org.bukkit.entity.Player;
import org.bukkit.entity.SkeletonHorse;
import org.bukkit.entity.ZombieHorse;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.FixedMetadataValue;

// ----------------------------------------------------------------------------
/**
Expand Down Expand Up @@ -253,123 +243,6 @@ public static String getAppearance(AbstractHorse abstractHorse) {
}
}

// ------------------------------------------------------------------------
/**
* Re-apply disguises to all disguised steeds when a player joins.
*/
public static void refreshSaddleDisguises() {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (onlinePlayer.getVehicle() instanceof AbstractHorse) {
AbstractHorse abstractHorse = (AbstractHorse) onlinePlayer.getVehicle();
EntityType disguiseEntityType = Util.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType != null) {
boolean showToRider = Util.isSaddleDisguiseVisibleToRider(abstractHorse);
Util.applySaddleDisguise(abstractHorse, onlinePlayer, disguiseEntityType, showToRider, false);
}
}
}
}

// ------------------------------------------------------------------------
/**
* Disguise a horse as a specified EntityType and notify a player when it is
* still disguised.
*
* @param abstractHorse the horse-like entity.
* @param rider the rider, to be notified if a disguise is applied.
* @param disguiseEntityType the EntityType of the disguise.
* @param showToRider if true, the disguise is visible to the rider.
* @param tellRider if true, tell the rider what disguise is in use.
*/
public static void applySaddleDisguise(AbstractHorse abstractHorse, Player rider, EntityType disguiseEntityType,
boolean showToRider, boolean tellRider) {
if (disguiseEntityType == null || EasyRider.PLUGIN.getDisguiseProvider() == null) {
return;
}

Set<Player> players = new HashSet<>(Bukkit.getOnlinePlayers());
if (!showToRider) {
players.remove(rider);
}
boolean validDisguise = EasyRider.PLUGIN.getDisguiseProvider().applyDisguise(abstractHorse, disguiseEntityType, players);
if (validDisguise) {
if (tellRider) {
rider.sendMessage(ChatColor.GOLD + "Your steed is disguised as " + disguiseEntityType + "!");
}

abstractHorse.removeMetadata(SELF_DISGUISE_KEY, EasyRider.PLUGIN);
if (showToRider) {
abstractHorse.setMetadata(SELF_DISGUISE_KEY, new FixedMetadataValue(EasyRider.PLUGIN, null));
}
} else {
Logger logger = EasyRider.PLUGIN.getLogger();
logger.warning("Horse " + abstractHorse.getUniqueId() + " accessed by " + rider.getName() +
" has a saddle with unsupported disguise " + disguiseEntityType + ".");
}
}

// --------------------------------------------------------------------------
/**
* Return true if the saddle disguise of the specified horse is visible to
* the rider.
*
* @param abstractHorse the horse-like entity.
* @return true if the saddle disguise of the specified horse is visible to
* the rider.
*/
public static boolean isSaddleDisguiseVisibleToRider(AbstractHorse abstractHorse) {
return !abstractHorse.getMetadata(SELF_DISGUISE_KEY).isEmpty();
}

// --------------------------------------------------------------------------
/**
* Return the EntityType of the disguise associated with a horse's saddle,
* or null if the saddle doesn't confer a disguise (or it's not a saddle).
*
* @param abstractHorse the horse-like entity.
* @return the EntityType of the disguise, or null if no disguise should be
* applied.
*/
public static EntityType getSaddleDisguiseType(AbstractHorse abstractHorse) {
ItemStack saddle = getSaddleItemStack(abstractHorse);
if (saddle == null || saddle.getType() != Material.SADDLE) {
return null;
}

ItemMeta meta = saddle.getItemMeta();
if (meta != null && meta.hasLore()) {
for (String lore : meta.getLore()) {
if (lore.startsWith(EasyRider.DISGUISE_PREFIX)) {
String entityTypeName = lore.substring(EasyRider.DISGUISE_PREFIX.length()).trim().toUpperCase();
try {
return EntityType.valueOf(entityTypeName);
} catch (IllegalArgumentException ex) {
}
}
}
}
return null;
}

// ------------------------------------------------------------------------
/**
* Get the ItemStack in the saddle slot of a horse, donkey or mule.
*
* @param abstractHorse the horse-like entity (includes llamas).
* @return the ItemStack in the saddle slot, or null if there is no saddle
* slot (as in the case of llamas).
*/
public static ItemStack getSaddleItemStack(AbstractHorse abstractHorse) {
if (abstractHorse instanceof Llama) {
return null;
}

// Mules, donkeys, zombie- and skeletal- horses do not have a
// HorseInventory. So get the saddle by ID rather than calling
// HorseInventory.getSaddle().
return abstractHorse.getInventory().getItem(0);
}

// ------------------------------------------------------------------------
/**
* Return the horizontal distance from a to b, ignoring Y coordinate
Expand Down Expand Up @@ -458,12 +331,4 @@ public static double linterp(double min, double max, double frac) {
private static final String[] STYLE_TO_APPEARANCE = {
"", ", socks", ", whitefield", ", white dots", ", black dots" };

/**
* Metadata key for metadata signifying that a saddle disguise is visible to
* the rider.
*
* If metadata with this key is absent, the rider cannot see the disguise.
*/
private static final String SELF_DISGUISE_KEY = "EasyRider_self_disguise";

} // class Util
8 changes: 4 additions & 4 deletions src/nu/nerd/easyrider/commands/HorseDisguiseSelfExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bukkit.entity.Player;

import nu.nerd.easyrider.EasyRider;
import nu.nerd.easyrider.Util;
import nu.nerd.easyrider.SpecialSaddles;

// ----------------------------------------------------------------------------
/**
Expand Down Expand Up @@ -52,13 +52,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (player.getVehicle() instanceof AbstractHorse) {
AbstractHorse abstractHorse = (AbstractHorse) player.getVehicle();
// Toggle disguise.
EntityType disguiseEntityType = Util.getSaddleDisguiseType(abstractHorse);
EntityType disguiseEntityType = SpecialSaddles.getSaddleDisguiseType(abstractHorse);
if (disguiseEntityType == null) {
sender.sendMessage(ChatColor.RED + "You must be riding a horse with a disguise saddle to see the disguise!");
return true;
}

boolean showToRider = !Util.isSaddleDisguiseVisibleToRider(abstractHorse);
boolean showToRider = !SpecialSaddles.isSaddleDisguiseVisibleToRider(abstractHorse);
if (showToRider) {
sender.sendMessage(ChatColor.GOLD + "You will now be able to see your steed's disguise.");
sender.sendMessage(ChatColor.GOLD + "Due to limitations in the Minecraft client, you can't move.");
Expand All @@ -67,7 +67,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(ChatColor.GOLD + "You can no longer see your steed's disguise.");
}
EasyRider.PLUGIN.getDisguiseProvider().removeDisguise(abstractHorse);
Util.applySaddleDisguise(abstractHorse, player, disguiseEntityType, showToRider, true);
SpecialSaddles.applySaddleDisguise(abstractHorse, player, disguiseEntityType, showToRider, true);

} else {
sender.sendMessage(ChatColor.RED + "You must be riding a horse with a disguise saddle to see the disguise!");
Expand Down

0 comments on commit 8088c31

Please sign in to comment.