Skip to content

Commit

Permalink
Added hack to determine wolf owner
Browse files Browse the repository at this point in the history
Added /putdown toggle command
  • Loading branch information
ashtonw committed Apr 5, 2011
1 parent 52eb48e commit e43ac61
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 50 deletions.
49 changes: 49 additions & 0 deletions src/main/java/com/jynxdaddy/wolfspawn/PutDownCommand.java
@@ -0,0 +1,49 @@
/**
*
*/
package com.jynxdaddy.wolfspawn;

import java.util.logging.Logger;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
* Handler for /putdown command
* @author Ashton
*
*/
public class PutDownCommand implements CommandExecutor {

@SuppressWarnings("unused")
private WolfSpawn plugin;
@SuppressWarnings("unused")
private static Logger log;

public PutDownCommand(WolfSpawn wolfSpawn) {
this.plugin = wolfSpawn;
PutDownCommand.log = WolfSpawn.log;
}

/* (non-Javadoc)
* @see org.bukkit.command.CommandExecutor#onCommand(org.bukkit.command.CommandSender, org.bukkit.command.Command, java.lang.String, java.lang.String[])
*/
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (plugin.addPutDownPlayer(player.getName())) {
player.sendMessage("[WolfSpawn] PutDown enabled - your wolves won't respawn");
}
else {
plugin.removePutDownPlayer(player.getName()); //toggle off
player.sendMessage("[WolfSpawn] PutDown disabled - your wolves will respawn again");
}
return true;
} else {
return false;
}
}

}
26 changes: 26 additions & 0 deletions src/main/java/com/jynxdaddy/wolfspawn/WPlayerListener.java
@@ -0,0 +1,26 @@
/**
*
*/
package com.jynxdaddy.wolfspawn;

import java.util.logging.Logger;

import org.bukkit.event.player.PlayerListener;

/**
* @author Ashton
*
*/
public class WPlayerListener extends PlayerListener {

@SuppressWarnings("unused")
private WolfSpawn plugin;
@SuppressWarnings("unused")
private static Logger log;

public WPlayerListener(WolfSpawn plugin) {
this.plugin = plugin;
WPlayerListener.log = WolfSpawn.log;
}

}
52 changes: 38 additions & 14 deletions src/main/java/com/jynxdaddy/wolfspawn/WolfListener.java
Expand Up @@ -5,7 +5,12 @@

import java.util.logging.Logger;

import net.minecraft.server.EntityWolf;
import net.minecraft.server.World;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.craftbukkit.entity.CraftWolf;
import org.bukkit.entity.CreatureType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
Expand All @@ -16,37 +21,56 @@

/**
* @author Ashton
*
*
*/
public class WolfListener extends EntityListener {


@SuppressWarnings("unused")
private WolfSpawn plugin;
@SuppressWarnings("unused")
private static Logger log;

public WolfListener(WolfSpawn plugin) {
this.plugin = plugin;
this.log = plugin.log;
WolfListener.log = WolfSpawn.log;
}

/* (non-Javadoc)
* @see org.bukkit.event.entity.EntityListener#onEntityDeath(org.bukkit.event.entity.EntityDeathEvent)
/*
* (non-Javadoc)
*
* @see
* org.bukkit.event.entity.EntityListener#onEntityDeath(org.bukkit.event
* .entity.EntityDeathEvent)
*/
@Override
public void onEntityDeath(EntityDeathEvent event) {
if (isWolf(event)) {
log.info("onEntityDeath Wolf");


Wolf wolf = (Wolf) event.getEntity();
Player owner; //= wolf.getOwner();

Location spawn = wolf.getWorld().getSpawnLocation();
LivingEntity newWolf = wolf.getWorld().spawnCreature(spawn, CreatureType.WOLF);
//newWolf.setOwner(owner);
//Workaround
EntityWolf mcwolf = ((CraftWolf) wolf).getHandle();
String owner = mcwolf.v();

if (plugin.isPutDownPlayer(owner)) {
Player player = plugin.getServer().getPlayer(owner);
if (player != null) player.sendMessage("[WolfSpawn] You put down your wolf");
}
else if (owner != null && owner.length() > 0) {
Location spawn = wolf.getWorld().getSpawnLocation();
LivingEntity newWolf = wolf.getWorld().spawnCreature(spawn,
CreatureType.WOLF);

EntityWolf newMcwolf = ((CraftWolf) newWolf).getHandle();
newMcwolf.a(owner); //setOwner
newMcwolf.d(true); // owned?
newMcwolf.b(true); // sitting

Player player = plugin.getServer().getPlayer(owner);
if (player != null) player.sendMessage("[WolfSpawn] Your wolf ran home");
}
}
}

private boolean isWolf(EntityEvent event) {
return event.getEntity() instanceof Wolf;
}
Expand Down
89 changes: 54 additions & 35 deletions src/main/java/com/jynxdaddy/wolfspawn/WolfSpawn.java
@@ -1,7 +1,8 @@

package com.jynxdaddy.wolfspawn;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Logger;

import org.bukkit.event.Event;
Expand All @@ -13,41 +14,59 @@

/**
* WolfSpawn
*
*
* @author ashtonw
*/
public class WolfSpawn extends JavaPlugin {
private final WolfListener wolfListener = new WolfListener(this);
//private final SamplePlayerListener playerListener = new SamplePlayerListener(this);
//private final SampleBlockListener blockListener = new SampleBlockListener(this);

public static Logger log = Logger.getLogger("Minecraft");
public Configuration cfg;

public void onDisable() {
PluginDescriptionFile pdfFile = this.getDescription();
log.info( pdfFile.getName() + " version " + pdfFile.getVersion() + " disabled!" );
}

public void onEnable() {
// Register our events
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.ENTITY_DEATH, wolfListener, Priority.Normal, this);

PluginDescriptionFile pdfFile = this.getDescription();
log.info( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );


File configFile = new File(this.getDataFolder(), "config.yml");
boolean existed = configFile.exists();
cfg = this.getConfiguration();



if (!existed)
cfg.setProperty("Test", true);
cfg.save();
}



private final WolfListener wolfListener = new WolfListener(this);
private final WPlayerListener playerListener = new WPlayerListener(this);

public static Logger log = Logger.getLogger("Minecraft");
public Configuration cfg;

private HashSet<String> putDownUsers = new HashSet<String>(10);

public void onDisable() {
PluginDescriptionFile pdfFile = this.getDescription();
log.info(pdfFile.getName() + " version " + pdfFile.getVersion()
+ " disabled!");
}

public void onEnable() {
// Register our events
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.ENTITY_DEATH, wolfListener,
Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener,
Priority.Normal, this);

//register commands
getCommand("putdown").setExecutor(new PutDownCommand(this));

PluginDescriptionFile pdfFile = this.getDescription();
log.info(pdfFile.getName() + " version " + pdfFile.getVersion()
+ " is enabled!");

File configFile = new File(this.getDataFolder(), "config.yml");
boolean existed = configFile.exists();
cfg = this.getConfiguration();

if (!existed)
cfg.setProperty("Test", true);
cfg.save();
}

public boolean addPutDownPlayer(String name) {
return putDownUsers.add(name);
}

public boolean isPutDownPlayer(String name) {
return putDownUsers.contains(name);
}

public boolean removePutDownPlayer(String name) {
return putDownUsers.remove(name);
}

}
4 changes: 3 additions & 1 deletion src/main/resources/plugin.yml
Expand Up @@ -2,4 +2,6 @@ name: WolfSpawn
main: com.jynxdaddy.wolfspawn.WolfSpawn
version: 0.1
commands:

putdown:
description: Enables player to disable respawn for his wolves.
usage: "Usage: '/putdown' to stop your wolves respawning."

0 comments on commit e43ac61

Please sign in to comment.