Merge branch 'api-changes' into dev

This commit is contained in:
Luis C.M. committed 2024-08-05 19:38:28 -05:00
commit beb556f827
5 files changed
+101 -21

No files matched your search

+11 -11
View File
@@ -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<Entity> 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);
@@ -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);
}
@@ -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);
}
}
+63 -1
View File
@@ -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<Integer, ScriptWorld> 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;
}
}
@@ -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;
}