From 4e8ff7ffbb9dd19ee2dba1b98e5f7f0779412f00 Mon Sep 17 00:00:00 2001 From: dfsek Date: Fri, 9 Oct 2020 17:49:29 -0700 Subject: [PATCH] Fix lootable rotation issues --- .../terra/population/StructurePopulator.java | 28 +++++++++++-------- .../com/dfsek/terra/structure/Structure.java | 9 ++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/dfsek/terra/population/StructurePopulator.java b/src/main/java/com/dfsek/terra/population/StructurePopulator.java index ea5d8c4df..12e1a8a82 100644 --- a/src/main/java/com/dfsek/terra/population/StructurePopulator.java +++ b/src/main/java/com/dfsek/terra/population/StructurePopulator.java @@ -48,17 +48,23 @@ public class StructurePopulator extends BlockPopulator { try(ProfileFuture ignore = TerraProfiler.fromWorld(world).measure("StructurePasteTime")) { struc.paste(spawn, chunk, rotation); for(StructureContainedInventory i : struc.getInventories()) { - Debug.info("Attempting to populate loot: " + i.getUid()); - Vector2 lootCoords = RotationUtil.getRotatedCoords(new Vector2(i.getX()-struc.getStructureInfo().getCenterX(), i.getZ()-struc.getStructureInfo().getCenterZ()), rotation); - Location inv = spawn.clone().add(lootCoords.getX(), 0, lootCoords.getZ()); - Debug.info(spawn.toString() + " became: " + inv.toString()); - Debug.info(Math.floorDiv(inv.getBlockX(), 16) + ":" + chunk.getX() + ", " + Math.floorDiv(inv.getBlockZ(), 16) + ":" + chunk.getZ() ); - if(Math.floorDiv(inv.getBlockX(), 16) != chunk.getX() || Math.floorDiv(inv.getBlockZ(), 16) != chunk.getZ()) continue; - Debug.info("Target is in chunk."); - LootTable table = conf.getLoot(i.getUid()); - if(table == null) continue; - Debug.info("Target has table assigned."); - table.fillInventory(((BlockInventoryHolder) inv.getBlock().getState()).getInventory(), random); + try { + Debug.info("Attempting to populate loot: " + i.getUid()); + Vector2 lootCoords = RotationUtil.getRotatedCoords(new Vector2(i.getX() - struc.getStructureInfo().getCenterX(), i.getZ() - struc.getStructureInfo().getCenterZ()), rotation.inverse()); + Location inv = spawn.clone().add(lootCoords.getX(), i.getY(), lootCoords.getZ()); + Debug.info(Math.floorDiv(inv.getBlockX(), 16) + ":" + chunk.getX() + ", " + Math.floorDiv(inv.getBlockZ(), 16) + ":" + chunk.getZ()); + if(Math.floorDiv(inv.getBlockX(), 16) != chunk.getX() || Math.floorDiv(inv.getBlockZ(), 16) != chunk.getZ()) + continue; + Debug.info("Target is in chunk."); + Debug.info(spawn.toString() + " became: " + inv.toString() + " (" + rotation + ", " + inv.getBlock().getType() + ")"); + LootTable table = conf.getLoot(i.getUid()); + if(table == null) continue; + Debug.info("Target has table assigned."); + table.fillInventory(((BlockInventoryHolder) inv.getBlock().getState()).getInventory(), random); + } catch(ClassCastException e) { + Debug.error("Could not populate structure loot!"); + Debug.stack(e); + } } break; } diff --git a/src/main/java/com/dfsek/terra/structure/Structure.java b/src/main/java/com/dfsek/terra/structure/Structure.java index 0502cfc7b..2082dab66 100644 --- a/src/main/java/com/dfsek/terra/structure/Structure.java +++ b/src/main/java/com/dfsek/terra/structure/Structure.java @@ -325,5 +325,14 @@ public class Structure implements Serializable { default: throw new IllegalArgumentException(); } } + public Rotation inverse() { + switch(this) { + case NONE: return NONE; + case CCW_90: return CW_90; + case CW_90: return CCW_90; + case CW_180: return CW_180; + default: throw new IllegalArgumentException(); + } + } } }