Skip to content

Commit

Permalink
2021/9/11 1.0.7
Browse files Browse the repository at this point in the history
* Kotlin and ForgeGradle 4
  • Loading branch information
MrMks committed Sep 11, 2021
1 parent fa6db92 commit a16863f
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 95 deletions.
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
buildscript {
repositories {
maven { url = "https://files.minecraftforge.net/maven" }
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:3.+'
classpath 'net.minecraftforge.gradle:ForgeGradle:4.1.+'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
}
}
apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: 'net.minecraftforge.gradle'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.


version = "1.0.6"
version = "1.0.7"
group = "moe.gensoukyo.npcspawner" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "NpcSpawner"

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
java.toolchain.languageVersion = JavaLanguageVersion.of(8)
//sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.

minecraft {
mappings channel: 'snapshot', version: '20171003-1.12'
Expand All @@ -31,6 +37,8 @@ minecraft {

server {

workingDirectory project.file('run')

// Recommended logging data for a userdev environment
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'

Expand All @@ -40,6 +48,9 @@ minecraft {
}
}

// Include resources generated by data generators.
sourceSets.main.resources { srcDir 'src/generated/resources' }

repositories {
flatDir { dirs 'libs' }
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
34 changes: 0 additions & 34 deletions src/main/java/moe/gensoukyo/npcspawner/NpcMob.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/moe/gensoukyo/npcspawner/NpcRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class NpcRegion {

public String name;
public Region2d region;
public Region3d region;
public String world;

/**
Expand All @@ -23,7 +23,7 @@ public static class MobSpawnRegion extends NpcRegion {
*/
public ArrayList<BlackListRegion> blackList;

public MobSpawnRegion(String name, Region2d region, int density, ArrayList<NpcMob> mobs, String world) {
public MobSpawnRegion(String name, Region3d region, int density, ArrayList<NpcMob> mobs, String world) {
this.name = name;
this.region = region;
this.density = density;
Expand All @@ -39,7 +39,7 @@ public MobSpawnRegion(String name, Region2d region, int density, ArrayList<NpcMo
public static class BlackListRegion extends NpcRegion {

public boolean delete;
public BlackListRegion(String name, Region2d region, boolean delete, String world) {
public BlackListRegion(String name, Region3d region, boolean delete, String world) {
this.name = name;
this.region = region;
this.delete = delete;
Expand Down
78 changes: 27 additions & 51 deletions src/main/java/moe/gensoukyo/npcspawner/NpcSpawner.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public class NpcSpawner {

static NpcSpawnerConfig config;
private static final Random random = new Random();
// the two cache array will occupy total 45Kb of memories: 8 Byte/double * 360 double / array * 2 array = 5760Byte = 46080 bits = 45k bits
private static final double[] ccos = new double[360];
private static final double[] csin = new double[360];

public static HashSet<String> blkList = new HashSet<>();
public static boolean enableBlkList = false;
Expand Down Expand Up @@ -68,19 +65,13 @@ public static void tryToSpawnMob(WorldServer worldServer) {
if (list.size() == 0) {
return;
}
label:
for (int i = random.nextInt(4); i < list.size(); i += 4) {
EntityPlayer player = list.get(i);
//在这个倒霉鬼周围随便找个地方
int r = config.minSpawnDistance + random.nextInt(config.maxSpawnDistance - config.minSpawnDistance);
int angle = random.nextInt(360);
if (ccos[angle] == 0 && csin[angle] == 0) {
double rad = Math.toRadians(angle);
ccos[angle] = Math.cos(rad);
csin[angle] = Math.sin(rad);
}
double x = player.posX + r * ccos[angle];
double z = player.posZ + r * csin[angle];
double[] cos_sin = MathUtils.unsafe_cos_sin(random.nextInt(360));
double x = player.posX + r * cos_sin[0];
double z = player.posZ + r * cos_sin[1];
double y;
//自下而上找个高度
double minY = Math.max(player.posY - 10, 0);
Expand All @@ -90,47 +81,32 @@ public static void tryToSpawnMob(WorldServer worldServer) {
findY = worldServer.getBlockState(new BlockPos(x, y, z)).getBlock() == Blocks.AIR;
}
if (!findY) continue;
Vec3d place = new Vec3d(x, y, z);

//选中的地点在不在某个刷怪区里
for (NpcRegion.MobSpawnRegion mobSpawnRegion : regions) {
//判断位置
Vec2d vec2d = new Vec2d(place.x, place.z);
//如果选中的地点周围怪太多,则不能生成
if (worldServer.getEntitiesWithinAABB(EntityCustomNpc.class,
new AxisAlignedBB(x - 50, y - 50, z - 50, x + 50, y + 50, z + 50)).size() >= mobSpawnRegion.density) {
continue;
}
if (anyPlayerInDistance(list, i, x, y, z, config.minSpawnDistance, config.minSpawnDisPow)) continue label;
//要在刷怪区内
if (mobSpawnRegion.region.isVecInRegion(vec2d) && mobSpawnRegion.region.isVecInRegion(new Vec2d(player.posX, player.posZ))) {
//如果在黑名单内,则不刷怪
for (NpcRegion.BlackListRegion blackListRegion : mobSpawnRegion.blackList) {
if (blackListRegion.region.isVecInRegion(vec2d)) {
continue label;
}
if (blackListRegion.region.isVecInRegion(new Vec2d(player.posX, player.posZ))) {
continue label;
}
}
//如果在刷怪区且不在黑名单内,随便挑一个怪物生成
boolean inWater = worldServer.getBlockState(new BlockPos(place.x, place.y - 1, place.z)).getBlock() == Blocks.WATER;
int timeOfDay = (int)(worldServer.getWorldTime() % 24000);
NpcMob mob = chooseMobToSpawn(mobSpawnRegion, inWater, timeOfDay);
if (mob != null) {
try {
IEntity<?> entity = NpcAPI.Instance().getClones().spawn(place.x, place.y, place.z,
mob.tab, mob.name, NpcAPI.Instance().getIWorld(worldServer));
if (entity instanceof ICustomNpc<?>) {
ICustomNpc<?> icn = (ICustomNpc<?>) entity;
if (icn.getStats().getRespawnType() < 3) icn.getStats().setRespawnType(4);
if (anyPlayerInDistance(list, i, x, y, z, config.minSpawnDistance, config.minSpawnDisPow)) continue;
final Vec3d place = new Vec3d(x, y, z);
final Vec3d pl_loc = player.getPositionVector();
final AxisAlignedBB aabb = new AxisAlignedBB(x - 50, y - 50, z - 50, x + 50, y + 50, z + 50);
final int densityNow = worldServer.getEntitiesWithinAABB(EntityCustomNpc.class, aabb).size();
regions.stream()
.filter(region -> densityNow < region.density
&& (region.region.isVecInRegion(pl_loc) && region.region.isVecInRegion(place))
&& region.blackList.stream().noneMatch(blkRegion -> blkRegion.region.isVecInRegion(pl_loc) || blkRegion.region.isVecInRegion(place)))
.findAny().ifPresent(mobSpawnRegion -> {
boolean inWater = worldServer.getBlockState(new BlockPos(place.x, place.y-1, place.z)).getBlock() == Blocks.WATER;
int timeOfDay = (int) (worldServer.getWorldTime() % 24000);
NpcMob mob = chooseMobToSpawn(mobSpawnRegion, inWater, timeOfDay);
if (mob != null) {
try {
IEntity<?> entity = NpcAPI.Instance().getClones().spawn(place.x, place.y, place.z,
mob.tab, mob.name, NpcAPI.Instance().getIWorld(worldServer));
if (entity instanceof ICustomNpc<?>) {
ICustomNpc<?> icn = (ICustomNpc<?>) entity;
if (icn.getStats().getRespawnType() < 3) icn.getStats().setRespawnType(4);
}
} catch (Exception e) {
ModMain.logger.info("NpcSpawner:NPC[" + mob.name + "]生成失败,可能是配置文件中提供的信息有误");
}
} catch (Exception e) {
ModMain.logger.info("NpcSpawner:NPC[" + mob.name + "]生成失败,可能是配置文件中提供的信息有误");
}
}
}
}
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/moe/gensoukyo/npcspawner/NpcSpawnerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public NpcRegion.MobSpawnRegion parseMobSpawnRegion(JsonObject mobSpawnRegionJso
JsonArray pos2 = mobSpawnRegionJson.get("pos2").getAsJsonArray();
int density = mobSpawnRegionJson.get("density").getAsInt();
JsonArray mobs = mobSpawnRegionJson.get("mobs").getAsJsonArray();
return new NpcRegion.MobSpawnRegion(name, new Region2d(pos1.get(0).getAsDouble(),
return new NpcRegion.MobSpawnRegion(name, new Region3d(pos1.get(0).getAsDouble(),
pos1.get(1).getAsDouble(), pos2.get(0).getAsDouble(), pos2.get(1).getAsDouble()), density, parseNpcMobs(mobs), world);
} catch (Exception e) {
ModMain.logger.error("MCGProject:刷怪配置解析错误!刷怪区信息不符合规范!已跳过该刷怪区!");
Expand All @@ -154,7 +154,7 @@ public NpcMob parseNpcMob(JsonObject mobJson) {
int tab = mobJson.get("tab").getAsInt();
String mobName = mobJson.get("name").getAsString();
double weight = mobJson.get("weight").getAsDouble();
NpcMob mob = new NpcMob(tab, mobName, weight);
NpcMob mob = new NpcMob(tab, mobName, (int) weight * 100);
if (mobJson.has("waterMob")) {
mob.setWaterMob(mobJson.get("waterMob").getAsBoolean());
}
Expand All @@ -175,7 +175,7 @@ public NpcRegion.BlackListRegion parseBlackListRegion(JsonObject blackListRegion
JsonArray pos1 = blackListRegionJson.get("pos1").getAsJsonArray();
JsonArray pos2 = blackListRegionJson.get("pos2").getAsJsonArray();
String world = blackListRegionJson.get("world").getAsString();
return new NpcRegion.BlackListRegion(name, new Region2d(pos1.get(0).getAsDouble(),
return new NpcRegion.BlackListRegion(name, new Region3d(pos1.get(0).getAsDouble(),
pos1.get(1).getAsDouble(), pos2.get(0).getAsDouble(), pos2.get(1).getAsDouble()), true, world);
} catch (Exception e) {
ModMain.logger.error("MCGProject:刷怪配置解析错误!安全区信息不符合规范!已跳过该安全区!");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/moe/gensoukyo/npcspawner/Region2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* @author SQwatermark
*/
@Deprecated
public class Region2d implements Cloneable {

public Vec2d vec1;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/moe/gensoukyo/npcspawner/Vec2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* @author SQwatermark
*/
@Deprecated
public class Vec2d {

public double x;
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/moe/gensoukyo/npcspawner/NpcMob.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package moe.gensoukyo.npcspawner

class NpcMob(@JvmField val tab : Int, @JvmField val name : String, @JvmField val weight : Int) {

@JvmField var waterMob : Boolean = false
@JvmField var timeStart : Int = 0
@JvmField var timeEnd: Int = 24000

fun setWaterMob(wm : Boolean) {
this.waterMob = wm
}

fun setTimeIndex(timeStart: Int, timeEnd: Int) {
this.timeStart = timeStart
this.timeEnd = timeEnd
}
}
55 changes: 55 additions & 0 deletions src/main/kotlin/moe/gensoukyo/npcspawner/Region3d.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package moe.gensoukyo.npcspawner

import net.minecraft.util.math.Vec3d

class Region3d {
@JvmField var vec1: Vec3d
@JvmField val vec2: Vec3d

constructor(v1: Vec3d, v2: Vec3d) {
val fx = v1.x < v2.x
val fy = v1.y < v2.y
val fz = v1.z < v2.z

if (fx && fy && fz) {
vec1 = v1
vec2 = v2
} else {
vec1 = Vec3d(if (fx) v1.x else v2.x, if (fy) v1.y else v2.y, if (fz) v1.z else v2.z)
vec2 = Vec3d(if (fx) v2.x else v1.x, if (fy) v2.y else v1.y, if (fz) v2.z else v1.z)
}
}

constructor(x1: Double, y1: Double, z1: Double, x2: Double, y2: Double, z2: Double){
val fx = x1 < x2
val fy = y1 < y2
val fz = z1 < z2
vec1 = Vec3d(if (fx) x1 else x2, if (fy) y1 else y2, if (fz) z1 else z2)
vec2 = Vec3d(if (fx) x2 else x1, if (fy) y2 else y1, if (fz) z2 else z1)
}

constructor(x1: Double, z1: Double, x2: Double, z2: Double) : this(x1, 0.0, z1, x2, 255.0, z2)
constructor(vec1: Vec2d, vec2: Vec2d) : this(vec1.x, vec1.y, vec2.x, vec2.y)

fun isVecInRegion(vec2d: Vec2d) = vecIn2d(vec2d.x, vec2d.y)

fun isVecInRegion(v: Vec3d) = vecIn2d(v.x, v.z) && v.y > vec1.y && v.y < vec2.y

private fun vecIn2d(x: Double, z: Double) = x > vec1.x && x < vec2.x && z > vec1.z && z < vec2.z

fun isCoincideWith(region: Region2d): Boolean {
val a1 = vec1
val a2 = vec2
val b1 = region.vec1
val b2 = region.vec2
return a2.x > b1.x && a1.x < b2.x && a1.z < b2.y && a2.z > b1.y
}

fun isCoincideWith(region: Region3d): Boolean {
val a1 = vec1
val a2 = vec2
val b1: Vec3d = region.vec1
val b2: Vec3d = region.vec2
return a2.x > b1.x && a1.x < b2.x && a2.y > b1.y && a1.y < b2.y && a2.z > b1.z && a1.z < b2.z
}
}

0 comments on commit a16863f

Please sign in to comment.