diff --git a/src/main/java/noppes/npcs/NPCSpawning.java b/src/main/java/noppes/npcs/NPCSpawning.java index f6e28878f..e95622242 100644 --- a/src/main/java/noppes/npcs/NPCSpawning.java +++ b/src/main/java/noppes/npcs/NPCSpawning.java @@ -82,12 +82,12 @@ public class NPCSpawning { SpawnData data = SpawnController.Instance.getRandomSpawnData(name, world.provider.dimensionId); if (data == null || !canCreatureTypeSpawnAtLocation(data, world, x, y, z) || world.getClosestPlayer(x, y, z, 24.0D) != null) continue; - + spawnData(data, world, x, y, z); } } } - + public static int countNPCs(World world){ int count = 0; List list = world.loadedEntityList; @@ -106,16 +106,16 @@ public class NPCSpawning { int i1 = world.rand.nextInt(chunk == null ? world.getActualHeight() : chunk.getTopFilledSegment() + 16 - 1); return new ChunkPosition(k, i1, l); } - + public static void performWorldGenSpawning(World world, int x, int z, Random rand){ BiomeGenBase biome = world.getBiomeGenForCoords(x + 8, z + 8); while (rand.nextFloat() < biome.getSpawningChance()){ SpawnData data = SpawnController.Instance.getRandomSpawnData(biome.biomeName, world.provider.dimensionId); if(data == null) continue; - + int size = 16; - + int j1 = x + rand.nextInt(size); int k1 = z + rand.nextInt(size); int l1 = j1; @@ -139,11 +139,11 @@ public class NPCSpawning { } else if(spawnData(data, world, j1, l2, k1)) break; - + } } } - + private static boolean spawnData(SpawnData data, World world, int x, int y, int z){ EntityLiving entityliving; @@ -177,7 +177,7 @@ public class NPCSpawning { } entityliving = (EntityLiving) spawnEntity; - + if(spawnEntity instanceof EntityCustomNpc){ EntityCustomNpc npc = (EntityCustomNpc) spawnEntity; npc.stats.spawnCycle = 3; @@ -198,14 +198,14 @@ public class NPCSpawning { if (canSpawn == Result.DENY || (canSpawn == Result.DEFAULT && !canEntitySpawn(data,entityliving))) return false; - CustomNPCsEvent.CNPCNaturalSpawnEvent event = new CustomNPCsEvent.CNPCNaturalSpawnEvent(data, NpcAPI.Instance().getIPos(new BlockPos(x,y,z)), NPCSpawning.animalSpawn, NPCSpawning.monsterSpawn, NPCSpawning.liquidSpawn, NPCSpawning.airSpawn); + CustomNPCsEvent.CNPCNaturalSpawnEvent event = new CustomNPCsEvent.CNPCNaturalSpawnEvent(entityliving, data, NpcAPI.Instance().getIPos(new BlockPos(x,y,z)), NPCSpawning.animalSpawn, NPCSpawning.monsterSpawn, NPCSpawning.liquidSpawn, NPCSpawning.airSpawn); if (EventHooks.onCNPCNaturalSpawn(event)) { return false; } entityliving.setPosition(event.attemptPosition.getX(),event.attemptPosition.getY(),event.attemptPosition.getZ()); world.spawnEntityInWorld(entityliving); - + return true; } @@ -216,7 +216,7 @@ public class NPCSpawning { return entityLiving.worldObj.checkNoEntityCollision(entityLiving.boundingBox) && entityLiving.worldObj.getCollidingBoundingBoxes(entityLiving, entityLiving.boundingBox).isEmpty(); } } - + public static boolean canCreatureTypeSpawnAtLocation(SpawnData data, World world, int x, int y, int z){ Block block = world.getBlock(x, y - 1, z); boolean hasSolidSurface = World.doesBlockHaveSolidTopSurface(world, x, y - 1, z); diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 1d69a86b1..080fd3c65 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -1258,6 +1258,12 @@ public class PacketHandlerServer{ player.addChatMessage(new ChatComponentText("Failed to create an entity out of your clone")); return; } + + if (entity instanceof EntityNPCInterface && !ConfigScript.canScript(player, CustomNpcsPermissions.SCRIPT)) { + EntityNPCInterface npc = (EntityNPCInterface) entity; + npc.script.setEnabled(false); + } + if(ConfigDebug.PlayerLogging && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER){ LogWriter.script(String.format("[%s] (Player) %s SPAWNED ENTITY %s", "CLONER", player.getCommandSenderName(), entity)); } @@ -1272,6 +1278,11 @@ public class PacketHandlerServer{ compound = ServerCloneController.Instance.getCloneData(player, Server.readString(buffer), buffer.readInt()); else compound = Server.readNBT(buffer); + + if (!ConfigScript.canScript(player, CustomNpcsPermissions.SCRIPT)) { + return; + } + if(compound != null) NoppesUtilServer.createMobSpawner(x, y, z, compound, player); } diff --git a/src/main/java/noppes/npcs/config/ConfigScript.java b/src/main/java/noppes/npcs/config/ConfigScript.java index 4d8a58165..0a342092e 100644 --- a/src/main/java/noppes/npcs/config/ConfigScript.java +++ b/src/main/java/noppes/npcs/config/ConfigScript.java @@ -169,16 +169,14 @@ public class ConfigScript } public static boolean isScriptDev(EntityPlayer player) { - return MinecraftServer.getServer().getConfigurationManager().func_152596_g(player.getGameProfile()) || - Developer.ScriptUser.contains(player.getUniqueID()) || - Developer.Universal.contains(player.getUniqueID()); + boolean isOp = MinecraftServer.getServer().getConfigurationManager().func_152596_g(player.getGameProfile()); + return (!ScriptOpsOnly || isOp) && + (Developer.ScriptUser.contains(player.getUniqueID()) || + Developer.Universal.contains(player.getUniqueID())); } public static boolean canScript(EntityPlayer player, CustomNpcsPermissions.Permission perm) { - if(ScriptOpsOnly){ - return isScriptDev(player); - } - - return CustomNpcsPermissions.hasPermission(player, perm); + boolean scriptDev = (!ScriptOpsOnly && Developer.ScriptUser.isEmpty()) || isScriptDev(player); + return scriptDev && CustomNpcsPermissions.hasPermission(player, perm); } } diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 2f0c4f5f6..ed1c02265 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -60,8 +60,13 @@ import noppes.npcs.scripted.item.*; import noppes.npcs.scripted.overlay.ScriptOverlay; import noppes.npcs.util.*; -import java.io.File; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class NpcAPI extends AbstractNpcAPI { private static final Map worldCache = new LRUHashMap<>(10); @@ -70,6 +75,9 @@ public class NpcAPI extends AbstractNpcAPI { public static final EventBus EVENT_BUS = new EventBus(); private static AbstractNpcAPI instance = null; + private final static String API_USER_AGENT = "CNPC+API"; + private static final ExecutorService executorService = Executors.newFixedThreadPool(10); + private NpcAPI() { } @@ -684,4 +692,58 @@ public class NpcAPI extends AbstractNpcAPI { part.setSmooth(smooth); return part; } + + public void postJsonHTTP(String url, String jsonPayload) { + this.postJsonHTTP(url, jsonPayload, API_USER_AGENT); + } + + public void postJsonHTTP(String url, String jsonPayload, String userAgent) { + this.postHTTP(url, jsonPayload, "application/json", userAgent); + } + + public void postHTTP(String url, String params, String contentType) { + this.postHTTP(url, params, contentType, API_USER_AGENT); + } + + public void postHTTP(String url, String params, String contentType, String userAgent) { + executorService.submit(() -> { + try { + HttpURLConnection con = NpcAPI.getConnection("POST", url, userAgent, contentType); + + // For POST only - set doOutput to true + con.setDoOutput(true); + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes(params); + wr.flush(); + wr.close(); + + int responseCode = con.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { // success + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + } + } catch (Exception ignored) { + } + }); + } + + private static HttpURLConnection getConnection(String requestMethod, String url, String userAgent, String contentType) throws IOException { + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + + con.setRequestMethod(requestMethod); + con.setRequestProperty("User-Agent", userAgent); + //con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); + if (!contentType.isEmpty()) { + con.setRequestProperty("Content-Type", contentType); + } + + return con; + } } diff --git a/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java b/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java index 0a0dfd2bb..1f95ec760 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomNPCsEvent.java @@ -2,12 +2,15 @@ package noppes.npcs.scripted.event; import cpw.mods.fml.common.eventhandler.Cancelable; import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.entity.EntityLivingBase; import noppes.npcs.api.AbstractNpcAPI; import noppes.npcs.api.IPos; import noppes.npcs.api.IWorld; +import noppes.npcs.api.entity.IEntityLivingBase; import noppes.npcs.api.event.ICustomNPCsEvent; import noppes.npcs.api.handler.data.INaturalSpawn; import noppes.npcs.constants.EnumScriptType; +import noppes.npcs.scripted.NpcAPI; public class CustomNPCsEvent extends Event implements ICustomNPCsEvent { public final AbstractNpcAPI API = AbstractNpcAPI.Instance(); @@ -27,6 +30,7 @@ public class CustomNPCsEvent extends Event implements ICustomNPCsEvent { @Cancelable public static class CNPCNaturalSpawnEvent extends CustomNPCsEvent implements ICustomNPCsEvent.CNPCNaturalSpawnEvent { + public final IEntityLivingBase entity; public final INaturalSpawn naturalSpawn; public IPos attemptPosition; public final boolean animalSpawnPassed; @@ -34,7 +38,8 @@ public class CustomNPCsEvent extends Event implements ICustomNPCsEvent { public final boolean liquidSpawnPassed; public final boolean airSpawnPassed; - public CNPCNaturalSpawnEvent(INaturalSpawn naturalSpawn, IPos attemptPosition, boolean animalSpawnPassed, boolean monsterSpawnPassed, boolean liquidSpawnPassed, boolean airSpawnPassed) { + public CNPCNaturalSpawnEvent(EntityLivingBase entity, INaturalSpawn naturalSpawn, IPos attemptPosition, boolean animalSpawnPassed, boolean monsterSpawnPassed, boolean liquidSpawnPassed, boolean airSpawnPassed) { + this.entity = (IEntityLivingBase) NpcAPI.Instance().getIEntity(entity); this.naturalSpawn = naturalSpawn; this.attemptPosition = attemptPosition; @@ -48,6 +53,10 @@ public class CustomNPCsEvent extends Event implements ICustomNPCsEvent { return EnumScriptType.CNPC_NATURAL_SPAWN.function; } + public IEntityLivingBase getEntity() { + return entity; + } + public INaturalSpawn getNaturalSpawn() { return this.naturalSpawn; }