Skip to content

Commit

Permalink
WolfPlus is now known as PetPlusa
Browse files Browse the repository at this point in the history
  • Loading branch information
warriordog committed May 11, 2016
1 parent e4afe1b commit cbbcdec
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 100 deletions.
8 changes: 8 additions & 0 deletions .idea/artifacts/PetPlus_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/artifacts/WolfPlus_jar.xml

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion WolfPlus.iml → PetPlus.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Spigot-1.9" level="application" />
<orderEntry type="library" name="Spigot-1.9.4" level="application" />
</component>
</module>
8 changes: 4 additions & 4 deletions res/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: WolfPlus
version: 0.2.0
description: Tweaks wolf behavior
name: PetsPlus
version: 0.0.1
description: Tweaks pet behavior
author: acomputerdog
main: net.acomputerdog.wolfplus.PluginWolfPlus
main: net.acomputerdog.petplus.PluginPetPlus
149 changes: 149 additions & 0 deletions src/net/acomputerdog/petplus/PluginPetPlus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package net.acomputerdog.petplus;

import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class PluginPetPlus extends JavaPlugin implements Listener {

private Player lastWolfBreeder;
private Player lastCatBreeder;
private boolean isLoaded = false;

@Override
public void onEnable() {
if (!isLoaded) {
getServer().getPluginManager().registerEvents(this, this);
isLoaded = true;
}
}

@Override
public void onDisable() {
lastWolfBreeder = null;
lastCatBreeder = null;
}


@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityHurt(EntityDamageEvent e) {
if (e.getEntityType() == EntityType.WOLF) {
onWolfHurt(e);
} else if (isTamedHorse(e.getEntity())) {
onHorseHurt(e);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityInteract(PlayerInteractEntityEvent e) {
Entity entity = e.getRightClicked();
if (entity.getType() == EntityType.WOLF) {
onRightClickWolf(e);
} else if (isCat(entity)) {
onRightClickCat(e);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntitySpawn(EntitySpawnEvent e) {
if (e.getEntityType() == EntityType.WOLF) {
onWolfSpawn(e);
} else if (isCat(e.getEntity())) {
onCatSpawn(e);
}
}

private boolean isCat(Entity e) {
return e.getType() == EntityType.OCELOT && ((Ocelot)e).getCatType() != Ocelot.Type.WILD_OCELOT;
}

private boolean isTamedHorse(Entity e) {
return e.getType() == EntityType.HORSE && ((Horse)e).isTamed();
}

/*
If a pet is right-clicked, save that player as the last breeder.
Not always accurate, but the timing would have to be extremely close for this to be an issue.
*/
private void onRightClickWolf(PlayerInteractEntityEvent e) {
lastWolfBreeder = e.getPlayer();
}

private void onRightClickCat(PlayerInteractEntityEvent e) {
lastCatBreeder = e.getPlayer();
}

/*
If a pet spawns and someone has recently bred a pet of that type, make them its owner
So that players can breed other players' wolves/cats and the babies will be theirs
*/
private void onWolfSpawn(EntitySpawnEvent e) {
if (lastWolfBreeder != null) {
Wolf wolf = (Wolf)e.getEntity();
wolf.setOwner(lastWolfBreeder);
lastWolfBreeder = null;
}
}

private void onCatSpawn(EntitySpawnEvent e) {
if (lastCatBreeder != null) {
Ocelot cat = (Ocelot)e.getEntity();
cat.setOwner(lastCatBreeder);
lastCatBreeder = null;
}
}

/*
1. If a player attacks their own wolf, cancel the event unless the wolf is sitting.
This stops players from hitting their own wolves in combat
2. If a wolf takes damage from an entity explosion, cancel the event.
Because creepers exploding your entire wolf pack SUCKS
3. Wolves cannot take damage from fire
*/
private void onWolfHurt(EntityDamageEvent e) {
Wolf wolf = (Wolf)e.getEntity();
if (e instanceof EntityDamageByEntityEvent) { //wolf hurt by entity
EntityDamageByEntityEvent e2 = (EntityDamageByEntityEvent)e;
Entity attacker = e2.getDamager();
if (attacker.getType() == EntityType.PLAYER) {
Player player = (Player) attacker;
if (wolf.getOwner().equals(player) && !wolf.isSitting()) {
e2.setCancelled(true);
}
} else if (attacker.getType() == EntityType.CREEPER) {
e2.setCancelled(true);
}
//wolf hurt by fire
} else if (e.getCause() == EntityDamageEvent.DamageCause.FIRE || e.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK) {
e.setCancelled(true);
}
}

/*
1. Ridden horses cannot take damage from arrows shot by their owner
2. Ridden horses don't take damage from fire
*/
private void onHorseHurt(EntityDamageEvent e) {
Horse horse = (Horse)e.getEntity();
Entity passenger = horse.getPassenger();
if (passenger instanceof Player) {
if (e instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent e2 = (EntityDamageByEntityEvent) e;
if (e2.getDamager() instanceof Arrow) {
Arrow arrow = (Arrow)e2.getDamager();
if (arrow.getShooter() == passenger) {
e.setCancelled(true);
}
}
} else if (e.getCause() == EntityDamageEvent.DamageCause.FIRE || e.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK) {
e.setCancelled(true);
}
}
}
}
85 changes: 0 additions & 85 deletions src/net/acomputerdog/wolfplus/PluginWolfPlus.java

This file was deleted.

0 comments on commit cbbcdec

Please sign in to comment.