Skip to content

Commit

Permalink
Spawn pet on worlguard's region (mob-spawning: false)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik authored and Nik committed Dec 21, 2017
1 parent 3a7920d commit 81b0937
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 66 deletions.
8 changes: 8 additions & 0 deletions .idea/artifacts/PetFaction_jar.xml

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

9 changes: 9 additions & 0 deletions .idea/libraries/worldedit_bukkit_6_1_7_2.xml

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

9 changes: 9 additions & 0 deletions .idea/libraries/worldguard.xml

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

2 changes: 2 additions & 0 deletions src/config.yml
Expand Up @@ -2,12 +2,14 @@
# Github: github.com/Niketion/PetFaction
faction-depend: true
list-commands:
- '&7&m+--------------------------------------------------+'
- '&a/pet gui &7- Open gui'
- '&a/pet shop &7- Open shop'
- '&a/pet here &7- Teletransport the pet into your position'
- '&a/pet change &7- Change your pet'
- '&a/pet name &7- Change name'
- '&a/pet away &7- Despawn pet'
- '&7&m+--------------------------------------------------+'
arguments-not-found: '&cArguments not found...'
duration-potion-pet: 10 #minutes
vip-pet: "&eBuy vip to buy this pet!"
Expand Down
17 changes: 16 additions & 1 deletion src/github/niketion/petfaction/Main.java
@@ -1,14 +1,19 @@
package github.niketion.petfaction;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import github.niketion.petfaction.command.CommandPet;
import github.niketion.petfaction.listener.ListenerPetFaction;
import github.niketion.petfaction.petfollow.*;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -269,4 +274,14 @@ public boolean hasPet(Player player) {
return false;
}
}

public WorldGuardPlugin getWorldGuard() {
Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");

if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
return null;
}

return (WorldGuardPlugin) plugin;
}
}
116 changes: 55 additions & 61 deletions src/github/niketion/petfaction/SpawnEntity.java
Expand Up @@ -9,47 +9,41 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

/**
This class has been designed to bypass the
"mob-spawning" of worldguard or other plugins
* This class has been designed to bypass the
* "mob-spawning" of worldguard or other plugins
*/
class SpawnEntity implements Listener {
public class SpawnEntity implements Listener {

private Main main = Main.getInstance();

private Player player;

/** Check if entity spawned is a pet */
private boolean isPet = false;
/**
* Check if entity spawned is a pet
*/
private static boolean isPet = false;

public static void setPet(boolean pet) {
isPet = pet;
}

SpawnEntity(Player player) {
this.player = player;
}

SpawnEntity() { }

@EventHandler(ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.getMessage().contains("/pet")) {
isPet = true;
main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
@Override
public void run() {
isPet = false;
}
}, 5L);
}
SpawnEntity() {
}

private boolean force = true;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSpawn(CreatureSpawnEvent event){
if (force){

@EventHandler(priority = EventPriority.MONITOR)
public void onSpawn(CreatureSpawnEvent event) {
if (force) {
if (isPet)
event.setCancelled(false);
}
Expand All @@ -59,56 +53,56 @@ void spawn() {
force = false;
//try {

FileConfiguration config = main.getConfig();
FileConfiguration petConfig = new FilePet(player).getPetConfig();
World worldPlayer = player.getWorld();
FileConfiguration config = main.getConfig();
FileConfiguration petConfig = new FilePet(player).getPetConfig();
World worldPlayer = player.getWorld();

for (World worlds : main.getServer().getWorlds())
for (Entity entities : worlds.getEntities())
if (entities.hasMetadata(player.getName())) {
entities.remove();
}
for (World worlds : main.getServer().getWorlds())
for (Entity entities : worlds.getEntities())
if (entities.hasMetadata(player.getName())) {
entities.remove();
}

if (main.petDeath.contains(player.getName())) {
player.sendMessage(main.getFormat(config.getString("pet-death").replaceAll("%number%", String.valueOf(config.getInt("pet-death-minutes")))));
return;
}
if (main.petDeath.contains(player.getName())) {
player.sendMessage(main.getFormat(config.getString("pet-death").replaceAll("%number%", String.valueOf(config.getInt("pet-death-minutes")))));
return;
}

String namePet = petConfig.getString("name");
String namePet = petConfig.getString("name");

// Get pet, set character
LivingEntity entity = (LivingEntity) worldPlayer.spawnEntity(player.getLocation(), EntityType.valueOf(petConfig.getString("pet")));
if (entity instanceof Ageable) {
((Ageable) entity).setBaby();
((Ageable) entity).setAgeLock(true);
}
// Get pet, set character
LivingEntity entity = (LivingEntity) worldPlayer.spawnEntity(player.getLocation(), EntityType.valueOf(petConfig.getString("pet")));
if (entity instanceof Ageable) {
((Ageable) entity).setBaby();
((Ageable) entity).setAgeLock(true);
}

double hearts = (double) config.getInt("gui.1.hearts." + petConfig.getInt("hearts")) * 2;
entity.setMaxHealth(hearts);
entity.setHealth(hearts);
double hearts = (double) config.getInt("gui.1.hearts." + petConfig.getInt("hearts")) * 2;
entity.setMaxHealth(hearts);
entity.setHealth(hearts);

if (!(petConfig.getInt("level") == 0)) {
for (PotionEffect effect : player.getActivePotionEffects())
player.removePotionEffect(effect.getType());
if (!(petConfig.getInt("level") == 0)) {
for (PotionEffect effect : player.getActivePotionEffects())
player.removePotionEffect(effect.getType());

ConfigurationSection sectionPotion = petConfig.getConfigurationSection("potion-pet");
ConfigurationSection sectionPotion = petConfig.getConfigurationSection("potion-pet");

if (sectionPotion != null)
for (String strings : sectionPotion.getKeys(false))
player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(strings), config.getInt("duration-potion-pet") * 60 * 20,
petConfig.getInt("potion-pet." + strings) - 1));
if (sectionPotion != null)
for (String strings : sectionPotion.getKeys(false))
player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(strings), config.getInt("duration-potion-pet") * 60 * 20,
petConfig.getInt("potion-pet." + strings) - 1));

}
}

if (namePet != null) {
entity.setCustomName(main.getFormat(namePet + config.getString("level-pet").replaceAll("%level%", String.valueOf(petConfig.getInt("level")))));
} else {
entity.setCustomName(main.getFormat(config.getString("default-name-pet").replaceAll("%player%", player.getName()) + " " + config.getString("level-pet").replaceAll("%level%",
String.valueOf(petConfig.getInt("level")))));
}
entity.setMetadata(player.getName(), new FixedMetadataValue(Main.getInstance(), "yes!"));
if (namePet != null) {
entity.setCustomName(main.getFormat(namePet + config.getString("level-pet").replaceAll("%level%", String.valueOf(petConfig.getInt("level")))));
} else {
entity.setCustomName(main.getFormat(config.getString("default-name-pet").replaceAll("%player%", player.getName()) + " " + config.getString("level-pet").replaceAll("%level%",
String.valueOf(petConfig.getInt("level")))));
}
entity.setMetadata(player.getName(), new FixedMetadataValue(Main.getInstance(), "yes!"));

main.getPetFollow(player, entity);
main.getPetFollow(player, entity);
//} catch (NullPointerException ignored) {}
force = true;
}
Expand Down
18 changes: 14 additions & 4 deletions src/github/niketion/petfaction/command/CommandPet.java
Expand Up @@ -2,10 +2,10 @@

import github.niketion.petfaction.Main;
import github.niketion.petfaction.Permissions;
import github.niketion.petfaction.SpawnEntity;
import github.niketion.petfaction.file.FilePet;
import github.niketion.petfaction.gui.GUI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
Expand All @@ -19,7 +19,8 @@
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.util.*;
import java.util.Arrays;
import java.util.List;

public class CommandPet implements CommandExecutor {

Expand All @@ -30,6 +31,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
return false;
}

SpawnEntity.setPet(true);
Main.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
@Override
public void run() {
SpawnEntity.setPet(false);
}
}, 5L);

assert commandSender instanceof Player;
Player player = (Player) commandSender;

Expand All @@ -54,6 +63,8 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
if (Main.getInstance().hasPet(player)) {
Main.getInstance().spawnPetHere(player);
}


return true;
case "away":
if (hasPermission(player, Permissions.COMMAND_AWAY))
Expand Down Expand Up @@ -111,15 +122,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
}

/**
* List arguments of command and credits
* List arguments
*
* @param commandSender - Who receives the message
*/
private void listArguments(CommandSender commandSender) {
for (String loopMessages : getConfig().getStringList("list-commands")) {
commandSender.sendMessage(format(loopMessages));
}
commandSender.sendMessage(ChatColor.DARK_AQUA+"Plugin developed by "+ChatColor.AQUA+"@Niketion");
}

/**
Expand Down

0 comments on commit 81b0937

Please sign in to comment.