From 4becbd8f45781ad3fa675a8f05c4804255a78551 Mon Sep 17 00:00:00 2001 From: Jkibbels Date: Sat, 25 Jan 2025 16:02:11 -0500 Subject: [PATCH] [factions-banking] More adjustments. Chunk load block, bug fixing, faction stuff extension --- .../FactionNodes/FactionBeacon.java | 4 +- .../BlockEntities/FactionBlockEntity.java | 71 ++++++++----------- .../CustomBlocks/Blocks/FactionBaseBlock.java | 13 ++-- .../FactionMgr/FactionConfig.java | 16 ++++- .../FactionMgr/FactionManager.java | 5 ++ .../keeblarcraft/mixin/EntitySpawnMixin.java | 4 +- 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java b/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java index 373e7ac..c035c31 100644 --- a/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java +++ b/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java @@ -17,9 +17,9 @@ import net.minecraft.util.ActionResult; public class FactionBeacon extends AbstractNode { private int beaconStrength = 1; // Increases with faction power; 1 is default private int potionEffectLastingTimeTicks = 200; // 20 ticks per second makes this 10 seconds - private Integer timerLengthMillis = 5000; // The default potion length is 10 seconds; so reset every 5 to be sure! + private final Integer timerLengthMillis = 5000; // The default potion length is 10 seconds; so reset every 5 to be sure! private Boolean resetTimer = true; - private Timer timer = new Timer(); + private final Timer timer = new Timer(); @Override public String GetNodeTitle() { return "faction_beacon"; diff --git a/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java b/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java index 1703158..7d66ed1 100644 --- a/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java +++ b/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java @@ -1,9 +1,11 @@ package jesse.keeblarcraft.CustomBlocks.BlockEntities; import java.util.ArrayList; +import java.util.HashMap; import jesse.keeblarcraft.AttributeMgr.AttributeMgr; import jesse.keeblarcraft.Callbacks.MobSpawnCallback; +import jesse.keeblarcraft.FactionMgr.FactionConfig; import jesse.keeblarcraft.FactionMgr.FactionManager; import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerEnteredBaseCallback; import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerExitedBaseCallback; @@ -45,7 +47,9 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan Boolean hasBuildFlight = true; Boolean hasSuperBeacon = true; Position3d storedBlockPos; + private final double factionDefaultRadiusBlocks = 50; // 50 blocks private ArrayList playersInRadius = new ArrayList<>(); + private HashMap factionPlayers; protected final PropertyDelegate propertyDelegate; @@ -53,7 +57,6 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan this(pos, state); this.faction = faction; - System.out.println("Subscribing to mob spawning"); MobSpawnCallback.EVENT.register((world, mob) -> { HandleMobSpawn(world, mob); return ActionResult.PASS; @@ -71,21 +74,17 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan @Override public int get(int index) { // The only value we need to get/delegate is faction power - return switch(index) { - default -> factionPower; - }; + return factionPower; } @Override public void set(int index, int value) { - switch(index) { - default -> factionPower = value; - }; + factionPower = value; } @Override public int size() { - // We are only sync'ing 1 integer - faction power + // We are only syncing 1 integer - faction power return 1; } @@ -137,22 +136,29 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan buf.writeBlockPos(this.pos); } + // This is uniquely only calculating the second coordinate on 'storedBlockPos' which is the center coordinate of the + // hypothetical sphere + private Boolean DistanceToCenterSphere(double x, double y, double z) { + // Distance between two points + double dist = Math.ceil(Math.sqrt( + Math.abs(Math.pow(Math.floor(x) - storedBlockPos.x, 2)) + + Math.abs(Math.pow(Math.floor(y) - storedBlockPos.y, 2)) + + Math.abs(Math.pow(Math.floor(z) - storedBlockPos.z, 2)))); + + // Arbitrarily for now; each point of faction power increases the distance by 10. Testing will confirm if this + // is too much or too little. Making this configurable would probably be good too + // Default radius; increased by power + double factionBlockRadius = factionDefaultRadiusBlocks + (10 * factionPower); + System.out.println("Dist: " + dist); + + return Math.ceil(dist) <= factionBlockRadius; + } + private Boolean IsPlayerInBounds(PlayerEntity player, BlockPos pos) { Boolean isNearBlock = false; String playerFaction = FactionManager.GetInstance().GetFactionOfPlayer(player.getUuidAsString()); - // Verify the player is in the faction to be considered "in bounds"! - if (playerFaction.equals(faction)) { - // Will check in range - assumes same world! 50 is a temporary value at the moment - Boolean xBounds = player.getX() <= pos.getX() + 50 && player.getX() >= pos.getX() - 50; - Boolean yBounds = player.getY() <= pos.getY() + 50 && player.getY() >= pos.getY() - 50; - Boolean zBounds = player.getZ() <= pos.getZ() + 50 && player.getZ() >= pos.getZ() - 50; - if (xBounds && yBounds && zBounds) { - isNearBlock = true; - } - } - - return isNearBlock; + return playerFaction.equals(faction) && DistanceToCenterSphere(player.getX(), player.getY(), player.getZ()); } // Call when the block attached to this entity is broken so we can send out our last messages to reset players correctly before perishing @@ -172,13 +178,14 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan // Tick method is called 20 times a second public void tick(World world, BlockPos pos, BlockState state) { - // For reasons unknown to me and only to Kaupenjoe (youtube video) - we never want to call this on a client. if (world.isClient() || faction == null) { return; } this.world = world; - + //TODO: The below values can ABSOLUTELY be moved to a one-time call-out when the block is created & when a player + // leaves or joins a faction with a simple callback subscriber. Leaving because not doing now, BUT REFACTOR + factionPlayers = FactionManager.GetInstance().GetFactionPlayers(faction); factionPower = FactionManager.GetInstance().GetFactionPower(faction); factionTier = FactionManager.GetInstance().GetFactionTier(faction); @@ -207,25 +214,5 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan ActionResult result = PlayerExitedBaseCallback.EVENT.invoker().interact(player, world, factionPower, factionTier); } } - - // if (stopMobSpawn) { - // // Temporary for now - // // Sphere center point is going to be X,Y,Z - // Integer X = pos.getX(); - // Integer y = pos.getY(); - // Integer z = pos.getZ(); - // Integer radius = 10; - // } - - // if (hasBuildFlight) { - // for (PlayerEntity player : world.getPlayers()) { - // if (IsPlayerInBounds(player, pos)) { - // // Notify the attribute tree to enable this attribute! - - // } - // } - - // world.getBlockEntity(pos.east()); - // } } } diff --git a/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java b/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java index b26125f..1a4d0d8 100644 --- a/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java +++ b/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java @@ -21,7 +21,6 @@ import net.minecraft.util.ItemScatterer; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraft.world.WorldAccess; public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProvider { // public static final VoxelShape SHAPE = Block.createCuboidShape(0, 0, 0, 16, 12, 16); @@ -57,10 +56,12 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv // Update block entity if (bEntity != null) { bEntity.SetFaction(fac); + // Enable chunk loading + world.getChunkManager().setChunkForced(world.getChunk(pos).getPos(), true); } // An empty string implies NO faction; only do things if it is NOT empty - if (!fac.equals("")) { + if (!fac.isEmpty()) { faction = fac; player.sendMessage(Text.of("This block now belongs to the " + faction + " faction.")); } else { @@ -81,7 +82,7 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { if (state.getBlock() != newState.getBlock()) { BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity != null && blockEntity instanceof FactionBlockEntity) { + if (blockEntity instanceof FactionBlockEntity) { ItemScatterer.spawn(world, pos, (FactionBlockEntity) blockEntity); world.updateComparators(pos, this); } @@ -93,8 +94,10 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv if (bEntity != null) { bEntity.ResetBlock(); } - } + }t + // Disable chunk loading + world.getChunkManager().setChunkForced(world.getChunk(pos).getPos(), false); super.onStateReplaced(state, world, pos, newState, moved); } } @@ -103,7 +106,7 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv // Calls ScreenHandler inside createMenu of entity class @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - // Server side handling is different than that of client side handling; we MUST check if we are on a server first because then we must + // Server side handling is different from that of client side handling; we MUST check if we are on a server first because then we must // request information about this block entity from the server System.out.println("onUse of faction base block called"); if (!world.isClient()) { diff --git a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java index 280d5ee..4abebfa 100644 --- a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java +++ b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java @@ -8,6 +8,8 @@ package jesse.keeblarcraft.FactionMgr; +import jesse.keeblarcraft.Utils.CommonStructures.Pair; + import static java.util.Map.entry; import java.util.ArrayList; @@ -48,7 +50,7 @@ public class FactionConfig { entry (3, VALID_FACTION_ROLES.OWNER) ); - public class WriteableFaction { + public static class WriteableFaction { // Key = Player UUID // Val = Faction role of player HashMap factionPlayerList = new HashMap(); @@ -133,6 +135,18 @@ public class FactionConfig { return members; } + // Gets faction members in a pair return; KEY = UUID values. PAIR = Ranks of each UUID + // Alternatively you could just call @see GetMemberNames(String) to get member names + // and their ranks. + public HashMap GetFactionPlayers(String factionName) { + HashMap players = null; + + if (IsValid(factionName)) { + players = allFactions.get(factionName).factionPlayerList; + } + return players; + } + ///////////////////////////////////////////////////////////////////////////// /// @fn DeleteFaction /// diff --git a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java index 08612d6..ecb4191 100644 --- a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java +++ b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java @@ -85,6 +85,7 @@ public class FactionManager { FactionTier.InitializeFactionTiers(); } + // This is purely a chat-based call. Here to help with `/faction info` basically. public void GetFactionInformation(ServerPlayerEntity player) { String factionName = GetFactionOfPlayer(player.getUuidAsString()); @@ -274,6 +275,10 @@ public class FactionManager { return success; } + public HashMap GetFactionPlayers(String factionName) { + return factionConfig.factions.GetFactionPlayers(factionName); + } + public Integer GetFactionPower(String factionName) { int fPower = 0; if (factionConfig.factions.IsValid(factionName)) { diff --git a/src/main/java/jesse/keeblarcraft/mixin/EntitySpawnMixin.java b/src/main/java/jesse/keeblarcraft/mixin/EntitySpawnMixin.java index e8c7c22..a7a617b 100644 --- a/src/main/java/jesse/keeblarcraft/mixin/EntitySpawnMixin.java +++ b/src/main/java/jesse/keeblarcraft/mixin/EntitySpawnMixin.java @@ -9,11 +9,11 @@ import org.spongepowered.asm.mixin.injection.At; import com.llamalad7.mixinextras.injector.ModifyReceiver; - import jesse.keeblarcraft.Callbacks.MobSpawnCallback; +import jesse.keeblarcraft.Callbacks.MobSpawnCallback; import net.minecraft.entity.EntityData; import net.minecraft.entity.SpawnReason; import net.minecraft.nbt.NbtCompound; - import net.minecraft.util.ActionResult; +import net.minecraft.util.ActionResult; import net.minecraft.world.LocalDifficulty; import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.SpawnHelper;