Skip to content

Commit

Permalink
Finished the code, ready for use
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonTG committed Sep 22, 2016
1 parent a63a13b commit d9e0ea1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 71 deletions.
Binary file added libs/LibsDisguises.jar
Binary file not shown.
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<id>dmulloy2-repo</id>
<url>http://repo.dmulloy2.net/content/groups/public/</url>
</repository>
<repository>
<id>md_5-public</id>
<url>http://repo.md-5.net/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -41,10 +45,11 @@
<systemPath>${project.basedir}/libs/PaperSpigot-1.8.8-R0.1-SNAPSHOT-latest.jar</systemPath>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>3.6.5</version>
<scope>provided</scope>
<groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId>
<version>9.0.7</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/LibsDisguises.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
<dependency>
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/leontg77/villagermobs/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
package com.leontg77.villagermobs;

import com.leontg77.villagermobs.commands.VillagerMobsCommand;
import com.leontg77.villagermobs.protocol.MobDisguiseAdapter;
import com.leontg77.villagermobs.listeners.MobListener;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -43,8 +45,10 @@ public class Main extends JavaPlugin {

@Override
public void onEnable() {
MobDisguiseAdapter disguise = new MobDisguiseAdapter(this);
VillagerMobsCommand cmd = new VillagerMobsCommand(this, disguise);
MobDisguise mobDisguise = new MobDisguise(DisguiseType.VILLAGER, true, true);

MobListener listener = new MobListener(mobDisguise);
VillagerMobsCommand cmd = new VillagerMobsCommand(this, listener, mobDisguise);

// register command.
getCommand("villagermobs").setExecutor(cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@

package com.leontg77.villagermobs.commands;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.google.common.collect.Lists;
import com.leontg77.villagermobs.Main;
import com.leontg77.villagermobs.protocol.MobDisguiseAdapter;
import com.leontg77.villagermobs.listeners.MobListener;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.HandlerList;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -49,14 +55,16 @@
public class VillagerMobsCommand implements CommandExecutor, TabCompleter {
private static final String PERMISSION = "villagermobs.manage";

private final MobDisguiseAdapter disguise;
private final MobListener listener;
private final Main plugin;

private final ProtocolManager manager = ProtocolLibrary.getProtocolManager();
private final Disguise disguise;

public VillagerMobsCommand(Main plugin, MobDisguiseAdapter disguise) {
this.disguise = disguise;
public VillagerMobsCommand(Main plugin, MobListener listener, Disguise disguise) {
this.plugin = plugin;

this.listener = listener;
this.disguise = disguise;
}

private boolean enabled = false;
Expand Down Expand Up @@ -90,7 +98,14 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
plugin.broadcast(Main.PREFIX + "Villager Mobs has been enabled.");
enabled = true;

manager.addPacketListener(disguise);
Bukkit.getPluginManager().registerEvents(listener, plugin);

Bukkit.getWorlds()
.forEach(world -> Arrays.stream(world.getLoadedChunks())
.forEach(chunk -> Arrays.stream(chunk.getEntities())
.filter(entity -> entity instanceof LivingEntity)
.filter(living -> !DisguiseAPI.isDisguised(living) || DisguiseAPI.getDisguise(living).getType() != DisguiseType.VILLAGER)
.forEach(living -> DisguiseAPI.disguiseToAll(living, disguise))));
return true;
}

Expand All @@ -108,7 +123,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
plugin.broadcast(Main.PREFIX + "Villager Mobs has been disabled.");
enabled = false;

manager.removePacketListener(disguise);
HandlerList.unregisterAll(listener);
return true;
}

Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/leontg77/villagermobs/listeners/MobListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Project: VillagerMobs
* Class: com.leontg77.villagermobs.listener.MobListener
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Leon Vaktskjold <leontg77@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.leontg77.villagermobs.listeners;

import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.Arrays;

/**
* Mob listener class.
*
* @author LeonTG77
*/
public class MobListener implements Listener {
private final Disguise disguise;

public MobListener(Disguise disguise) {
this.disguise = disguise;
}

@EventHandler
public void on(PlayerJoinEvent event) {
Player player = event.getPlayer();
DisguiseAPI.disguiseToAll(player, disguise);
}

@EventHandler
public void on(CreatureSpawnEvent event) {
LivingEntity entity = event.getEntity();
DisguiseAPI.disguiseToAll(entity, disguise);
}

@EventHandler
public void on(ChunkLoadEvent event) {
Arrays.stream(event.getChunk().getEntities())
.filter(entity -> entity instanceof LivingEntity)
.filter(living -> !DisguiseAPI.isDisguised(living) || DisguiseAPI.getDisguise(living).getType() != DisguiseType.VILLAGER)
.forEach(living -> DisguiseAPI.disguiseToAll(living, disguise));
}
}

This file was deleted.

0 comments on commit d9e0ea1

Please sign in to comment.