Skip to content

Commit

Permalink
Added ability to equip DeadHorses with barding, which is taken into a…
Browse files Browse the repository at this point in the history
…ccount during damage calculations.
  • Loading branch information
EasyMFnE committed Oct 30, 2014
1 parent db7a720 commit cbb1e3d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 10 deletions.
29 changes: 20 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.easymfne.plugins</groupId>
<artifactId>DeadHorses</artifactId>
<version>1.1</version>
<name>${project.artifactId}</name>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.easymfne.plugins</groupId>
<artifactId>DeadHorses</artifactId>
<version>1.2</version>
<name>${project.artifactId}</name>
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
Expand Down Expand Up @@ -69,16 +70,21 @@
</plugin>
</plugins>
</build>
<repositories>
<repositories>
<repository>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</snapshots>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
<repository>
<id>comphenix-rep</id>
<name>Comphenix Repository</name>
<url>http://repo.comphenix.net/content/groups/public</url>
</repository>
<repository>
<id>Plugin Metrics</id>
<url>http://repo.mcstats.org/content/repositories/public</url>
Expand All @@ -88,10 +94,15 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>LATEST</version>
<version>1.7.10-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
Expand Down
147 changes: 147 additions & 0 deletions src/main/java/net/easymfne/deadhorses/BardingListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* This file is part of the DeadHorses plugin by EasyMFnE.
*
* DeadHorses is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* DeadHorses is distributed in the hope that it will be useful, but without any warranty; without
* even the implied warranty of merchantability or fitness for a particular purpose. See the GNU
* General Public License for details.
*
* You should have received a copy of the GNU General Public License v3 along with DeadHorses. If
* not, see <http://www.gnu.org/licenses/>.
*/
package net.easymfne.deadhorses;

import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

/**
* The class that monitors and reacts to horse inventory events.
*
* @author Eric Hildebrand
*/
public class BardingListener implements Listener {

public static final double DIAMOND_BARDING_MODIFIER = 0.56;
public static final double GOLD_BARDING_MODIFIER = 0.72;
public static final double IRON_BARDING_MODIFIER = 0.8;
public static final double NO_BARDING_MODIFIER = 1.0;

private DeadHorses plugin = null;

/**
* Instantiate by getting a reference to the plugin instance and registering each of the defined
* EventHandlers.
*
* @param plugin Reference to DeadHorses plugin instance
*/
public BardingListener(DeadHorses plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

/**
* Unregister all registered EventHandlers, preventing further reactions.
*/
public void close() {
HandlerList.unregisterAll(this);
}

/**
* @param entity The Horse
* @return Whether or not the Horse is a Skeleton or Undead.
*/
private static boolean isDeadHorse(Entity entity) {
return entity instanceof Horse
&& (((Horse) entity).getVariant() == Horse.Variant.SKELETON_HORSE || ((Horse) entity)
.getVariant() == Horse.Variant.UNDEAD_HORSE);
}

private boolean isBarding(ItemStack item) {
return item != null
&& (item.getType() == Material.IRON_BARDING || item.getType() == Material.GOLD_BARDING || item
.getType() == Material.DIAMOND_BARDING);
}

private double getBardingModifier(ItemStack item) {
if (item != null) {
switch (item.getType()) {
case DIAMOND_BARDING:
return DIAMOND_BARDING_MODIFIER;
case GOLD_BARDING:
return GOLD_BARDING_MODIFIER;
case IRON_BARDING:
return IRON_BARDING_MODIFIER;
default:
}
}
return NO_BARDING_MODIFIER;
}

private boolean isEmpty(ItemStack item) {
return item == null || item.getType() == Material.AIR;
}

@EventHandler(ignoreCancelled = true)
public void onDeadHorseDamage(EntityDamageEvent event) {
if (isDeadHorse(event.getEntity())
&& isBarding(((Horse) event.getEntity()).getInventory().getArmor())) {
Horse horse = (Horse) event.getEntity();
event.setDamage(event.getDamage() * getBardingModifier(horse.getInventory().getArmor()));
}
}

@SuppressWarnings("deprecation") // TODO: Remove suppression... never?
@EventHandler
public void onDeadHorseInventoryClick(InventoryClickEvent event) {
InventoryHolder topHolder = event.getView().getTopInventory().getHolder();
if (topHolder instanceof Horse && isDeadHorse((Horse) topHolder)) {
Horse horse = (Horse) topHolder;
if (!plugin.getPluginConfig().isArmorEquippable(horse.getVariant())) {
return;
}
boolean needUpdate = false;

// Shift-click Barding item in bottom inventory.
if (event.getClick().isShiftClick() && isBarding(event.getCurrentItem())
&& isEmpty(horse.getInventory().getArmor())) {
plugin.fancyLog("Shift-click barding in inventory.");
event.setCancelled(true);
horse.getInventory().setArmor(event.getCurrentItem());
event.setCurrentItem(new ItemStack(Material.AIR));
needUpdate = true;
}
// Click empty horse armor slot with cursor holding Barding
else if (event.getInventory() == event.getView().getTopInventory() && event.getRawSlot() == 1
&& isEmpty(event.getCurrentItem()) && isBarding(event.getCursor())) {
plugin.fancyLog("Click empty armor slot while holding barding.");
event.setCancelled(true);
horse.getInventory().setArmor(event.getCursor());
event.setCursor(new ItemStack(Material.AIR));
needUpdate = true;
}

if (needUpdate) {
for (HumanEntity viewer : event.getViewers()) {
if (viewer.getType() == EntityType.PLAYER) {
((Player) viewer).updateInventory();
}
}
}
}
}

}
8 changes: 8 additions & 0 deletions src/main/java/net/easymfne/deadhorses/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public double getFoodChance(Horse.Variant variant, ItemStack item) {
return 0.0;
}

/**
* @param variant A variant of horse
* @return Whether the horse variant should be allowed to equip barding
*/
public boolean isArmorEquippable(Horse.Variant variant) {
return plugin.getConfig().getBoolean("armor." + variant.name().toLowerCase(), false);
}

/**
* Examine an itemstack and determine if a specific horse Variant can eat it
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/easymfne/deadhorses/DeadHorses.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DeadHorses extends JavaPlugin {

private Config config = null;
private DeadHorsesCommand deadHorsesCommand = null;
private BardingListener bardingListener = null;
private PlayerListener playerListener = null;

/* Strings for fancyLog() methods */
Expand Down Expand Up @@ -81,6 +82,8 @@ public void onDisable() {
fancyLog("=== DISABLE START ===");
playerListener.close();
playerListener = null;
bardingListener.close();
bardingListener = null;
deadHorsesCommand.close();
deadHorsesCommand = null;
config = null;
Expand All @@ -105,6 +108,7 @@ public void onEnable() {

config = new Config(this);
deadHorsesCommand = new DeadHorsesCommand(this);
bardingListener = new BardingListener(this);
playerListener = new PlayerListener(this);
startMetrics();
fancyLog("=== ENABLE COMPLETE (" + (Calendar.getInstance().getTimeInMillis() - start)
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# GENERAL SETTINGS
##################

armor:
skeleton_horse: true
undead_horse: true
feeding-can-age: true
leashing: true
taming:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: DeadHorses
version: '1.1'
version: '1.2'
description: Allow players to tame and ride undead horses!

author: EasyMFnE
Expand Down

0 comments on commit cbb1e3d

Please sign in to comment.