Update to 1.20.2 NMS

This commit is contained in:
tastybento 2023-12-03 17:33:29 -08:00
parent d9b395f234
commit 97f9ff2674
2 changed files with 374 additions and 338 deletions

View File

@ -58,7 +58,7 @@
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.20.1-R0.1-SNAPSHOT</spigot.version>
<spigot.version>1.20.2-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>

View File

@ -3,7 +3,14 @@ package world.bentobox.boxed.listeners;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -20,7 +27,7 @@ import org.bukkit.block.structure.Mirror;
import org.bukkit.block.structure.StructureRotation;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
@ -54,14 +61,15 @@ import world.bentobox.boxed.objects.BoxedStructureBlock;
import world.bentobox.boxed.objects.IslandStructures;
/**
* @author tastybento
* Place structures in areas after they are created
* @author tastybento Place structures in areas after they are created
*/
public class NewAreaListener implements Listener {
/**
* Structure record contains the name of the structure, the structure itself, where it was placed and
* enums for rotation, mirror, and a flag to paste mobs or not.
* Structure record contains the name of the structure, the structure itself,
* where it was placed and enums for rotation, mirror, and a flag to paste mobs
* or not.
*
* @param name - name of structure
* @param structure - Structure object
* @param location - location where it has been placed
@ -69,19 +77,23 @@ public class NewAreaListener implements Listener {
* @param mirror - mirror setting
* @param noMobs - if false, mobs not pasted
*/
public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot, Mirror mirror, Boolean noMobs) {}
public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot,
Mirror mirror, Boolean noMobs) {
}
private static final Map<Integer, EntityType> BUTCHER_ANIMALS = Map.of(0, EntityType.COW, 1, EntityType.SHEEP, 2, EntityType.PIG);
private static final List<BlockFace> CARDINALS = List.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
private static final List<String> JAR_STRUCTURES = List.of("bee", "pillager", "polar_bear", "axolotl", "allay", "parrot", "frog");
private static final Map<Integer, EntityType> BUTCHER_ANIMALS = Map.of(0, EntityType.COW, 1, EntityType.SHEEP, 2,
EntityType.PIG);
private static final List<BlockFace> CARDINALS = List.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST,
BlockFace.WEST);
private static final List<String> JAR_STRUCTURES = List.of("bee", "pillager", "polar_bear", "axolotl", "allay",
"parrot", "frog");
private static final List<String> STRUCTURES = List.of("ancient_city", "bastion_remnant", "bastion",
"buried_treasure", "desert_pyramid", "end_city",
"fortress", "igloo", "jungle_pyramid", "mansion", "mineshaft", "mineshaft_mesa",
"monument", "nether_fossil", "ocean_ruin_cold", "ocean_ruin_warm", "pillager_outpost",
"ruined_portal_desert", "ruined_portal_jungle", "ruined_portal_mountain", "ruined_portal_nether",
"ruined_portal_ocean", "ruined_portal_swamp", "ruined_portal", "shipwreck_beached",
"shipwreck", "stronghold", "swamp_hut", "village_desert", "village_plains",
"village_savanna", "village_snowy", "village_taiga");
"buried_treasure", "desert_pyramid", "end_city", "fortress", "igloo", "jungle_pyramid", "mansion",
"mineshaft", "mineshaft_mesa", "monument", "nether_fossil", "ocean_ruin_cold", "ocean_ruin_warm",
"pillager_outpost", "ruined_portal_desert", "ruined_portal_jungle", "ruined_portal_mountain",
"ruined_portal_nether", "ruined_portal_ocean", "ruined_portal_swamp", "ruined_portal", "shipwreck_beached",
"shipwreck", "stronghold", "swamp_hut", "village_desert", "village_plains", "village_savanna",
"village_snowy", "village_taiga");
private final Boxed addon;
private final File structureFile;
private final Queue<StructureRecord> itemsToBuild = new LinkedList<>();
@ -94,8 +106,6 @@ public class NewAreaListener implements Listener {
private final Database<IslandStructures> handler;
private final Map<String, IslandStructures> islandStructureCache = new HashMap<>();
/**
* @param addon addon
*/
@ -132,7 +142,8 @@ public class NewAreaListener implements Listener {
* Build something in the queue
*/
private void buildStructure() {
// Only kick off a build if there is something to build and something isn't already being built
// Only kick off a build if there is something to build and something isn't
// already being built
if (!pasting && !itemsToBuild.isEmpty()) {
// Build item
StructureRecord item = itemsToBuild.poll();
@ -141,9 +152,10 @@ public class NewAreaListener implements Listener {
}
/**
* Load known structures from the templates file.
* This makes them available for admins to use in the boxed place file.
* If this is not done, then no structures (templates) will be available.
* Load known structures from the templates file. This makes them available for
* admins to use in the boxed place file. If this is not done, then no
* structures (templates) will be available.
*
* @param event BentoBoxReadyEvent
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@ -164,6 +176,7 @@ public class NewAreaListener implements Listener {
/**
* Track if a place has entered a structure.
*
* @param e PlayerMoveEvent
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@ -178,16 +191,18 @@ public class NewAreaListener implements Listener {
final String islandId = island.getUniqueId();
IslandStructures is = getIslandStructData(islandId);
// Check if player is in any of the structures
Map<BoundingBox, String> structures = e.getTo().getWorld().getEnvironment().equals(Environment.NETHER) ?
is.getNetherStructureBoundingBoxMap():is.getStructureBoundingBoxMap();
for (Map.Entry<BoundingBox, String> en:structures.entrySet()) {
Map<BoundingBox, String> structures = e.getTo().getWorld().getEnvironment().equals(Environment.NETHER)
? is.getNetherStructureBoundingBoxMap()
: is.getStructureBoundingBoxMap();
for (Map.Entry<BoundingBox, String> en : structures.entrySet()) {
if (en.getKey().contains(e.getTo().toVector())) {
for (String s: STRUCTURES) {
for (String s : STRUCTURES) {
if (en.getValue().startsWith(s)) {
giveAdvFromCriteria(e.getPlayer(), s);
}
}
//STRUCTURES.stream().filter(en.getValue()::startsWith).forEach(s -> giveAdvFromCriteria(e.getPlayer(), s));
// STRUCTURES.stream().filter(en.getValue()::startsWith).forEach(s ->
// giveAdvFromCriteria(e.getPlayer(), s));
}
}
});
@ -195,6 +210,7 @@ public class NewAreaListener implements Listener {
/**
* Gives a player all the advancements that have string as a named criteria
*
* @param player - player
* @param string - criteria
*/
@ -202,7 +218,7 @@ public class NewAreaListener implements Listener {
// Give every advancement that requires a bastion
Bukkit.advancementIterator().forEachRemaining(ad -> {
if (!player.getAdvancementProgress(ad).isDone()) {
for (String crit: ad.getCriteria()) {
for (String crit : ad.getCriteria()) {
if (crit.equals(string)) {
// Set the criteria (it may not complete the advancement completely
player.getAdvancementProgress(ad).awardCriteria(crit);
@ -216,6 +232,7 @@ public class NewAreaListener implements Listener {
/**
* Get all the known island structures for this island
*
* @param islandId - island ID
* @return IslandStructures
*/
@ -225,7 +242,8 @@ public class NewAreaListener implements Listener {
return islandStructureCache.get(islandId);
}
// Get from database
IslandStructures struct = handler.objectExists(islandId) ? handler.loadObject(islandId) : new IslandStructures(islandId);
IslandStructures struct = handler.objectExists(islandId) ? handler.loadObject(islandId)
: new IslandStructures(islandId);
this.islandStructureCache.put(islandId, struct);
return struct;
}
@ -245,7 +263,8 @@ public class NewAreaListener implements Listener {
if (!(addon.inWorld(island.getWorld()))) {
return;
}
// Load the latest config so that admins can change it on the fly without reloading
// Load the latest config so that admins can change it on the fly without
// reloading
YamlConfiguration config = YamlConfiguration.loadConfiguration(structureFile);
Location center = island.getProtectionCenter();
for (String env : config.getKeys(false)) {
@ -274,7 +293,8 @@ public class NewAreaListener implements Listener {
String[] split = name.split(",");
if (split.length > 1) {
// Rotation
rot = Enums.getIfPresent(StructureRotation.class, split[1].strip().toUpperCase(Locale.ENGLISH)).or(StructureRotation.NONE);
rot = Enums.getIfPresent(StructureRotation.class, split[1].strip().toUpperCase(Locale.ENGLISH))
.or(StructureRotation.NONE);
name = split[0];
}
if (split.length == 3) {
@ -309,7 +329,8 @@ public class NewAreaListener implements Listener {
pasting = true;
// Place the structure
item.structure().place(item.location(), true, item.rot(), item.mirror(), -1, 1, rand);
addon.log(item.name() + " placed at " + item.location().getWorld().getName() + " " + Util.xyz(item.location().toVector()));
addon.log(item.name() + " placed at " + item.location().getWorld().getName() + " "
+ Util.xyz(item.location().toVector()));
// Remove any jigsaw artifacts
BoundingBox bb = removeJigsaw(item);
// Store it for future reference
@ -327,7 +348,9 @@ public class NewAreaListener implements Listener {
}
/**
* Removes Jigsaw blocks from a placed structure. Fills underwater ruins with water.
* Removes Jigsaw blocks from a placed structure. Fills underwater ruins with
* water.
*
* @param item - record of what's required
* @return the resulting bounding box of the structure
*/
@ -339,13 +362,17 @@ public class NewAreaListener implements Listener {
Location otherCorner = switch (structureRotation) {
case CLOCKWISE_180 -> loc.clone().add(new Vector(-structure.getSize().getX(), structure.getSize().getY(), -structure.getSize().getZ()));
case CLOCKWISE_180 -> loc.clone()
.add(new Vector(-structure.getSize().getX(), structure.getSize().getY(), -structure.getSize().getZ()));
case CLOCKWISE_90 -> loc.clone().add(new Vector(-structure.getSize().getZ(), structure.getSize().getY(), structure.getSize().getX()));
case CLOCKWISE_90 -> loc.clone()
.add(new Vector(-structure.getSize().getZ(), structure.getSize().getY(), structure.getSize().getX()));
case COUNTERCLOCKWISE_90 -> loc.clone().add(new Vector(structure.getSize().getZ(), structure.getSize().getY(), -structure.getSize().getX()));
case COUNTERCLOCKWISE_90 -> loc.clone()
.add(new Vector(structure.getSize().getZ(), structure.getSize().getY(), -structure.getSize().getX()));
case NONE -> loc.clone().add(new Vector(structure.getSize().getX(), structure.getSize().getY(), structure.getSize().getZ()));
case NONE -> loc.clone()
.add(new Vector(structure.getSize().getX(), structure.getSize().getY(), structure.getSize().getZ()));
};
@ -370,12 +397,12 @@ public class NewAreaListener implements Listener {
return bb;
}
/**
* Process a structure block. Sets it to a structure void at a minimum.
* If the structure block has metadata indicating it is a chest, then it will fill
* the chest with a buried treasure loot. If it is waterlogged, then it will change
* Process a structure block. Sets it to a structure void at a minimum. If the
* structure block has metadata indicating it is a chest, then it will fill the
* chest with a buried treasure loot. If it is waterlogged, then it will change
* the void to water.
*
* @param b structure block
*/
private static void processStructureBlock(Block b) {
@ -388,7 +415,7 @@ public class NewAreaListener implements Listener {
if (bsb.getMetadata().contains("chest")) {
Block downBlock = b.getRelative(BlockFace.DOWN);
if (downBlock.getType().equals(Material.CHEST)) {
Chest chest = (Chest)downBlock.getState();
Chest chest = (Chest) downBlock.getState();
// TODO: for now just give treasure
chest.setLootTable(LootTables.BURIED_TREASURE.getLootTable());
chest.update();
@ -413,9 +440,9 @@ public class NewAreaListener implements Listener {
}
private static void spawnMob(Block b, BoxedJigsawBlock bjb) {
// bjb.getPool contains a lot more than just mobs, so we have to filter it to see if any mobs are in there. This list may need to grow in the future
EntityType type =
switch (bjb.getPool()) {
// bjb.getPool contains a lot more than just mobs, so we have to filter it to
// see if any mobs are in there. This list may need to grow in the future
EntityType type = switch (bjb.getPool()) {
case "minecraft:bastion/mobs/piglin" -> EntityType.PIGLIN;
case "minecraft:bastion/mobs/hoglin" -> EntityType.HOGLIN;
case "minecraft:bastion/mobs/piglin_melee" -> EntityType.PIGLIN_BRUTE;
@ -425,7 +452,8 @@ public class NewAreaListener implements Listener {
case "minecraft:village/common/pigs" -> EntityType.PIG;
case "minecraft:village/common/cows" -> EntityType.COW;
case "minecraft:village/common/iron_golem" -> EntityType.IRON_GOLEM;
case "minecraft:village/common/butcher_animals", "minecraft:village/common/animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3));
case "minecraft:village/common/butcher_animals", "minecraft:village/common/animals" ->
BUTCHER_ANIMALS.get(rand.nextInt(3));
default -> null;
};
// Boxed
@ -439,22 +467,25 @@ public class NewAreaListener implements Listener {
} else if (bjb.getPool().contains("villagers")) {
type = EntityType.VILLAGER;
}
//if (type == null) {
// if (type == null) {
// BentoBox.getInstance().logDebug(bjb.getPool());
//}
// }
// Spawn it
if (type != null) {
Entity e = b.getWorld().spawnEntity(b.getRelative(BlockFace.UP).getLocation(), type);
if (e != null) {
e.setPersistent(true);
}
//BentoBox.getInstance().logDebug("Spawned a " + type + " at " + b.getRelative(BlockFace.UP).getLocation());
// BentoBox.getInstance().logDebug("Spawned a " + type + " at " +
// b.getRelative(BlockFace.UP).getLocation());
}
}
/**
* Corrects the direction of a block based on the structure's rotation
* @param finalState - the final block state of the block, which may include a facing: direction
*
* @param finalState - the final block state of the block, which may include a
* facing: direction
* @param sr - the structure's rotation
* @return a rewritten blockstate with the updated direction, if required
*/
@ -469,12 +500,14 @@ public class NewAreaListener implements Listener {
// No change - shouldn't happen, but just in case
return finalState;
}
return finalState.replace(oldDirection.name().toLowerCase(Locale.ENGLISH), newDirection.name().toLowerCase(Locale.ENGLISH));
return finalState.replace(oldDirection.name().toLowerCase(Locale.ENGLISH),
newDirection.name().toLowerCase(Locale.ENGLISH));
}
/**
* Adjusts the direction based on the StructureRotation
*
* @param oldDirection the old direction to adjust
* @param sr the structure rotation
* @return the new direction, or SELF if something weird happens
@ -483,7 +516,7 @@ public class NewAreaListener implements Listener {
if (sr.equals(StructureRotation.CLOCKWISE_180)) {
return oldDirection.getOppositeFace();
} else if (sr.equals(StructureRotation.CLOCKWISE_90)) {
return switch(oldDirection) {
return switch (oldDirection) {
case EAST -> BlockFace.SOUTH;
case NORTH -> BlockFace.EAST;
case SOUTH -> BlockFace.WEST;
@ -491,7 +524,7 @@ public class NewAreaListener implements Listener {
default -> BlockFace.SELF;
};
} else if (sr.equals(StructureRotation.COUNTERCLOCKWISE_90)) {
return switch(oldDirection) {
return switch (oldDirection) {
case EAST -> BlockFace.NORTH;
case NORTH -> BlockFace.WEST;
case SOUTH -> BlockFace.EAST;
@ -504,11 +537,13 @@ public class NewAreaListener implements Listener {
/**
* Looks for north, south, east, west in the blockstate.
*
* @param finalState - the final block state of the block
* @return direction, if found, otherwise SELF
*/
private static BlockFace getDirection(String finalState) {
return CARDINALS.stream().filter(bf -> finalState.contains(bf.name().toLowerCase(Locale.ENGLISH))).findFirst().orElse(BlockFace.SELF);
return CARDINALS.stream().filter(bf -> finalState.contains(bf.name().toLowerCase(Locale.ENGLISH))).findFirst()
.orElse(BlockFace.SELF);
}
private static String nmsData(Block block) {
@ -517,7 +552,8 @@ public class NewAreaListener implements Listener {
// for 1.13+ (we have use WorldServer)
TileEntity te = cw.getHandle().c_(new BlockPosition(w.getBlockX(), w.getBlockY(), w.getBlockZ()));
try {
PacketPlayOutTileEntityData packet = ((PacketPlayOutTileEntityData) te.h()); // get update packet from NMS object
PacketPlayOutTileEntityData packet = ((PacketPlayOutTileEntityData) te.h()); // get update packet from NMS
// object
// here we should use reflection because "c" field isn't accessible
Field f = packet.getClass().getDeclaredField("c"); // get field
f.setAccessible(true); // make it available