Collision check before spawning

This commit is contained in:
Minecon724 2024-11-10 09:26:37 +01:00
parent be38934c3b
commit 0f88955ea8
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
3 changed files with 56 additions and 8 deletions

View file

@ -151,13 +151,35 @@ public class GiantProcessor implements Listener {
} }
configuration.effects.forEach(entity::addPotionEffect); configuration.effects.forEach(entity::addPotionEffect);
//trackedGiants.add((Giant) entity);
logger.fine("Spawned a Giant at " + pos); logger.fine("Spawned a Giant at " + pos);
return entity; return entity;
} }
/**
* The check is very approximate
*
* @param location the location
* @return whether a giant can be spawned here
*/
public boolean isSpawnableAt(Location location) {
for (int y=0; y<=12; y++) {
if (!location.clone().add(0, y, 0).getBlock().isEmpty()) // isPassable also seems good
return false;
}
return true;
}
public LivingEntity spawnGiantIfPossible(Location location) {
if (isSpawnableAt(location)) {
return spawnGiant(location);
} else {
return null;
}
}
@EventHandler @EventHandler
public void onChunkLoad(ChunkLoadEvent event) { public void onChunkLoad(ChunkLoadEvent event) {
Entity[] entities = event.getChunk().getEntities(); Entity[] entities = event.getChunk().getEntities();
@ -194,10 +216,10 @@ public class GiantProcessor implements Listener {
if (e.getEntityType() == EntityType.ZOMBIE) { if (e.getEntityType() == EntityType.ZOMBIE) {
if (configuration.chance > random.nextDouble()) { if (configuration.chance > random.nextDouble()) {
e.setCancelled(true);
spawnGiant(e.getLocation());
logger.fine("Spawned a Giant by chance at " + e.getLocation()); logger.fine("Spawned a Giant by chance at " + e.getLocation());
if (spawnGiantIfPossible(e.getLocation()) != null) {
e.setCancelled(true);
}
} }
} }
} }

View file

@ -47,8 +47,11 @@ public class GiantsCommand implements CommandExecutor {
if (action.equals("spawn")) { if (action.equals("spawn")) {
if (player != null) { if (player != null) {
plugin.spawnGiant(player.getLocation()); if (plugin.spawnGiantIfPossible(player.getLocation()) == null) {
sender.sendMessage("No space here for a Giant");
} else {
sender.sendMessage("Spawned a Giant"); sender.sendMessage("Spawned a Giant");
}
} else { } else {
sender.sendMessage("Only players can use this command."); sender.sendMessage("Only players can use this command.");
} }

View file

@ -48,10 +48,11 @@ public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
); );
} catch (IOException e) { } catch (IOException e) {
getLogger().warning(e.getMessage()); getLogger().warning(e.getMessage());
getLogger().warning("Failed checking JAR signature. This is not important right now, but it usually indicates future problems."); getLogger().warning("Failed checking JAR signature. This is not important right now, but it usually indicates future problems. If this persists, re-download the JAR from SpigotMC.");
} catch (JarVerifier.VerificationException e) { } catch (JarVerifier.VerificationException e) {
getLogger().warning(e.getMessage()); getLogger().warning(e.getMessage());
getLogger().warning("Plugin JAR is of invalid signature. Please re-download the JAR."); getLogger().warning("Plugin JAR is of invalid signature. If this persists, re-download the JAR from SpigotMC.");
getLogger().warning("Did you update from 2.0.7? If yes, you must re-download 2.0.9+ from SpigotMC, then delete plugins/.paper-remapped");
} }
/* bStats is optional. not anymore /* bStats is optional. not anymore
@ -79,4 +80,26 @@ public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
public LivingEntity spawnGiant(Location location) { public LivingEntity spawnGiant(Location location) {
return giantProcessor.spawnGiant(location); return giantProcessor.spawnGiant(location);
} }
/**
* Checks if a giant can be spawned at a location<br>
* The check is very approximate, but works for most scenarios
*
* @param location The location
* @return Whether a giant can be spawned
*/
public boolean isSpawnableAt(Location location) {
return giantProcessor.isSpawnableAt(location);
}
/**
* Checks if a giant can be spawned at a location and spawns it<br>
* * The check is very approximate, but works for most scenarios
*
* @param location The location
* @return The spawned {@link Giant} or null if no room for it
*/
public LivingEntity spawnGiantIfPossible(Location location) {
return giantProcessor.spawnGiantIfPossible(location);
}
} }