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 e4917bd..373e7ac 100644 --- a/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java +++ b/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionBeacon.java @@ -67,7 +67,6 @@ public class FactionBeacon extends AbstractNode { // player.setAbsorptionAmount(absorptionAmnt); // Duration is in ticks beaconStrength = GetBeaconAmplifier(tier); - System.out.println("Beacon strength is " + beaconStrength); // Tier 2 effects if (FactionTier.IsGreaterOrEqualTo(tier, FactionTierEnum.TIER_II)) { diff --git a/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionFlight.java b/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionFlight.java index 82e3f19..aac3786 100644 --- a/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionFlight.java +++ b/src/main/java/jesse/keeblarcraft/AttributeMgr/AttributeNodes/FactionNodes/FactionFlight.java @@ -89,8 +89,6 @@ public class FactionFlight extends AbstractNode { }); PlayerInBaseCallback.EVENT.register((player, world, power, factionTier) -> { - System.out.println("Faction power is " + power); - System.out.println("Faction tier is " + factionTier.name()); // Make sure player can fly while inside the border. We don't ever want to run this more than once! if (!canFly && !loginInBaseToggle) { player.sendMessage(Text.of("Faction flight enabled")); diff --git a/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMenu.java b/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMenu.java index 16b5cf5..54a6454 100644 --- a/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMenu.java +++ b/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMenu.java @@ -34,6 +34,10 @@ public class ChatMenu { this.header = header; } + public void SetHeader(ChatMsg msg) { + this.header = msg.regularText; + } + public void SetLeftArrow(Text leftArrow) { this.leftArrow = leftArrow; } @@ -50,6 +54,10 @@ public class ChatMenu { AddMsg(Text.of(newMsg)); } + public void AddMsg(ChatMsg newMsg) { + AddMsg(newMsg.regularText); + } + public void ClearList() { msgList.clear(); pageCount = 1; @@ -64,5 +72,20 @@ public class ChatMenu { public void SendMsg(ServerPlayerEntity target) { // Calculate number of pages pageCount = (int) Math.ceil(msgList.size() / (double) pageLimit); + + // Send the header + target.sendMessage(header); + target.sendMessage(Text.of("")); // Spacer + + // Send the body + int msgIndex = 0; + for (int page = 0; page < pageCount; page++) { + for (int i = 0; i < pageLimit; i++) { + target.sendMessage(msgList.get(msgIndex++)); + } + } + + // Send the footer + // target.sendMessage(footer); } } diff --git a/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMsg.java b/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMsg.java index c5d0cf3..087387e 100644 --- a/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMsg.java +++ b/src/main/java/jesse/keeblarcraft/ChatStuff/ChatMsg.java @@ -19,6 +19,9 @@ import net.minecraft.text.Style; import net.minecraft.text.Text; public class ChatMsg { + // May be null; store last message of each type so class can be referenced as object + MutableText mutableText; + Text regularText; ///////////////////////////////////////////////////////////////////////////// /// @fn MakeCopyableTxt /// @@ -93,6 +96,8 @@ public class ChatMsg { System.out.println("Value of copyAbleText: " + copyableText.getString()); System.out.println("Value of testTxt: " + testTxt.getString()); + mutableText = testTxt; + return testTxt; } @@ -130,7 +135,8 @@ public class ChatMsg { /// first with functions inside this class. ///////////////////////////////////////////////////////////////////////////// public Text ColorMsg(Text msg, COLOR_CODE color) { - return Text.of(ChatFormatting.GetColor(color) + msg.getString() + ChatFormatting.COLOR_END); + regularText = Text.of(ChatFormatting.GetColor(color) + msg.getString() + ChatFormatting.COLOR_END); + return regularText; } ///////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/jesse/keeblarcraft/Commands/FactionCommands.java b/src/main/java/jesse/keeblarcraft/Commands/FactionCommands.java index 6702358..37b0ef6 100644 --- a/src/main/java/jesse/keeblarcraft/Commands/FactionCommands.java +++ b/src/main/java/jesse/keeblarcraft/Commands/FactionCommands.java @@ -37,6 +37,7 @@ public class FactionCommands { var set = CommandManager.literal("set").build(); var get = CommandManager.literal("get").build(); var power = CommandManager.literal("power").build(); + var join = CommandManager.literal("join").build(); var info = CommandManager.literal("info") .executes(context -> GetFactionInformation(context.getSource().getPlayer())) .build(); @@ -61,6 +62,10 @@ public class FactionCommands { ) ).build(); + var joinName = CommandManager.argument("faction_name", StringArgumentType.string()) + .executes(context -> JoinFaction(context.getSource().getPlayer(), StringArgumentType.getString(context, "faction_name"))) + .build(); + var promoteName = CommandManager.argument("target_name", EntityArgumentType.player()) .executes(context -> PromotePlayerInFaction(context, EntityArgumentType.getPlayer(context, "target_name"))) .build(); @@ -106,6 +111,9 @@ public class FactionCommands { factionNode.addChild(fly); factionNode.addChild(set); factionNode.addChild(get); + factionNode.addChild(join); + + join.addChild(joinName); get.addChild(power); power.addChild(getPowerName); @@ -171,6 +179,11 @@ public class FactionCommands { return 0; } + public int JoinFaction(ServerPlayerEntity player, String factionName) { + FactionManager.GetInstance().JoinFaction(player, factionName); + return 0; + } + public int GetFactionPower(ServerPlayerEntity player) { FactionManager.GetInstance().GetFactionPower(player); return 0; @@ -244,7 +257,9 @@ public class FactionCommands { return 0; } + // Returns information on the players current faction private int GetFactionInformation(ServerPlayerEntity player) { + FactionManager.GetInstance().GetFactionInformation(player); return 0; } diff --git a/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java b/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java index 19e0c1f..524d868 100644 --- a/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java +++ b/src/main/java/jesse/keeblarcraft/CustomBlocks/BlockEntities/FactionBlockEntity.java @@ -3,13 +3,13 @@ package jesse.keeblarcraft.CustomBlocks.BlockEntities; import java.util.ArrayList; import jesse.keeblarcraft.AttributeMgr.AttributeMgr; -import jesse.keeblarcraft.AttributeMgr.AttributeTree; import jesse.keeblarcraft.FactionMgr.FactionManager; import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerEnteredBaseCallback; import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerExitedBaseCallback; import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerInBaseCallback; import jesse.keeblarcraft.FactionMgr.FactionTier.FactionTierEnum; import jesse.keeblarcraft.GuiMgr.FactionBlockScreenHandler; +import jesse.keeblarcraft.Utils.CommonStructures.Position3d; import jesse.keeblarcraft.world.ImplementedInventory; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.block.BlockState; @@ -42,6 +42,7 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan Boolean stopMobSpawn = true; Boolean hasBuildFlight = true; Boolean hasSuperBeacon = true; + Position3d storedBlockPos; private ArrayList playersInRadius = new ArrayList<>(); protected final PropertyDelegate propertyDelegate; @@ -57,6 +58,7 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan public FactionBlockEntity(BlockPos pos, BlockState state) { super(BlockEntityRegistration.FACTION_BLOCK_ENTITY, pos, state); + storedBlockPos = new Position3d(pos.getX(), pos.getY(), pos.getZ()); this.propertyDelegate = new PropertyDelegate() { @Override public int get(int index) { @@ -120,18 +122,37 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan private Boolean IsPlayerInBounds(PlayerEntity player, BlockPos pos) { Boolean isNearBlock = false; + String playerFaction = FactionManager.GetInstance().GetFactionOfPlayer(player.getUuidAsString()); - // 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; + // 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; } + // Call when the block attached to this entity is broken so we can send out our last messages to reset players correctly before perishing + public void ResetBlock() { + System.out.println("Reset block called. Is world null? " + (world==null?"YES":"NO")); + if (world != null) { + for (PlayerEntity player : world.getPlayers()) { + Boolean isPlayerInFactionRadius = IsPlayerInBounds(player, pos); + if (isPlayerInFactionRadius) { + ActionResult result = PlayerExitedBaseCallback.EVENT.invoker().interact(player, world, factionPower, factionTier); + } + } + } else { + System.out.println("Error, world is null and factionblockentity cannot reset players!"); + } + } + // 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. @@ -139,16 +160,16 @@ public class FactionBlockEntity extends BlockEntity implements ExtendedScreenHan return; } + this.world = world; + factionPower = FactionManager.GetInstance().GetFactionPower(faction); factionTier = FactionManager.GetInstance().GetFactionTier(faction); - // TODO: Optimize this block so that when it is placed the placers UUID is related to a faction and only pull from a list of those players not the entire server for (PlayerEntity player : world.getPlayers()) { Boolean isPlayerInFactionRadius = IsPlayerInBounds(player, pos); if (isPlayerInFactionRadius) { // Run individual faction modules for players here // First time entry callback check - System.out.println("FACTION IS " + faction); if (!playersInRadius.contains(player.getUuidAsString())) { playersInRadius.add(player.getUuidAsString()); ActionResult result = PlayerEnteredBaseCallback.EVENT.invoker().interact(player, world, factionPower, factionTier); diff --git a/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java b/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java index 52459a9..b26125f 100644 --- a/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java +++ b/src/main/java/jesse/keeblarcraft/CustomBlocks/Blocks/FactionBaseBlock.java @@ -21,6 +21,7 @@ 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); @@ -42,7 +43,7 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv @Override public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { - if (world.isClient) { + if (world.isClient()) { return; } @@ -84,6 +85,16 @@ public class FactionBaseBlock extends BlockWithEntity implements BlockEntityProv ItemScatterer.spawn(world, pos, (FactionBlockEntity) blockEntity); world.updateComparators(pos, this); } + + // This is pulled directly from #AbstractBlock$AbstractBlockState.class:onStateReplaced + // Catch if the block is being broken. Update our block entity to reset all + if (state.hasBlockEntity() && !state.isOf(newState.getBlock())) { + FactionBlockEntity bEntity = (FactionBlockEntity) world.getBlockEntity(pos); + if (bEntity != null) { + bEntity.ResetBlock(); + } + } + super.onStateReplaced(state, world, pos, newState, moved); } } diff --git a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java index e2a573c..b1e59ae 100644 --- a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java +++ b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionConfig.java @@ -57,6 +57,7 @@ public class FactionConfig { HashMap DISPLAY_ONLY_LIST = new HashMap(); // List contains UUID of players who are openly invited to this faction + // TODO: Refactor to map so players can un-invite by name not UUID... List openInvites = new ArrayList(); Integer factionBankBalance; @@ -112,6 +113,26 @@ public class FactionConfig { return amount; } + public Integer GetBankBalance(String factionName) { + Integer amount = 0; + if (IsValid(factionName)) { + amount = allFactions.get(factionName).factionBankBalance; + } + + return amount; + } + + // You should probably verify the player is in the faction before calling this so the object is not empty + public HashMap GetMemberNames(String factionName) { + HashMap members = new HashMap(); + + if (IsValid(factionName)) { + members = allFactions.get(factionName).DISPLAY_ONLY_LIST; + } + + return members; + } + ///////////////////////////////////////////////////////////////////////////// /// @fn DeleteFaction /// @@ -204,6 +225,7 @@ public class FactionConfig { if (allFactions.containsKey(factionName)) { allFactions.get(factionName).factionPlayerList.put(playerUuid, VALID_FACTION_ROLES.EMPLOYEE); allFactions.get(factionName).DISPLAY_ONLY_LIST.put(playerDisplayName, VALID_FACTION_ROLES.EMPLOYEE); + allFactions.get(factionName).openInvites.remove(playerUuid); // Remove them from the invite list if they joined } } diff --git a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java index 6259d7d..9a80288 100644 --- a/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java +++ b/src/main/java/jesse/keeblarcraft/FactionMgr/FactionManager.java @@ -9,12 +9,19 @@ package jesse.keeblarcraft.FactionMgr; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map.Entry; import jesse.keeblarcraft.ConfigMgr.ConfigManager; +import jesse.keeblarcraft.FactionMgr.FactionConfig.VALID_FACTION_ROLES; import jesse.keeblarcraft.FactionMgr.FactionTier.FactionTierEnum; import jesse.keeblarcraft.MailSystem.MailMgr; import jesse.keeblarcraft.Keeblarcraft; +import jesse.keeblarcraft.ChatStuff.ChatMenu; +import jesse.keeblarcraft.ChatStuff.ChatMsg; +import jesse.keeblarcraft.ChatStuff.ChatFormatting.COLOR_CODE; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; @@ -78,6 +85,46 @@ public class FactionManager { FactionTier.InitializeFactionTiers(); } + public void GetFactionInformation(ServerPlayerEntity player) { + String factionName = GetFactionOfPlayer(player.getUuidAsString()); + + if (!factionName.equals("")) { + Integer fPower = factionConfig.factions.GetPower(factionName); + HashMap displayNames = factionConfig.factions.GetMemberNames(factionName); + Integer fBankBalance = factionConfig.factions.GetBankBalance(factionName); + + System.out.println((fPower == null ? "YES":"NO") + " " + (displayNames == null ? "YES":"NO") + " " + (fBankBalance == null ? "YES":"NO")); + + ArrayList nameMapToList = new ArrayList(); + ChatMsg chatPlayerList = new ChatMsg(); + for (Entry entry : displayNames.entrySet()) { + nameMapToList.add(chatPlayerList.ColorMsg(entry.getKey() + " - " + entry.getValue().name(), COLOR_CODE.BLUE)); + } + + System.out.println("name map null? " + nameMapToList == null ? "YES":"NO"); + + // Let's make our chat objects first so things are copyable and whatnot + ChatMsg chatHeader = new ChatMsg(); + chatHeader.ColorMsg(Text.of(factionName), COLOR_CODE.GOLD); + + ChatMsg chatBalance = new ChatMsg(); + chatBalance.ColorMsg(Text.of(Integer.toString(fBankBalance)), COLOR_CODE.GREEN); + + ChatMsg chatPower = new ChatMsg(); + chatPower.ColorMsg(Text.of(Integer.toString(fPower)), COLOR_CODE.RED); + + ChatMenu chatBlock = new ChatMenu(); + chatBlock.SetHeader(chatHeader); + chatBlock.AddMsg(chatPlayerList); + chatBlock.AddMsg(chatBalance); + chatBlock.AddMsg(chatPower); + + chatBlock.SendMsg(player); + } else { + player.sendMessage(Text.of("You aren't in a faction!")); + } + } + ///////////////////////////////////////////////////////////////////////////// /// @fn LeaveFaction /// @@ -162,6 +209,18 @@ public class FactionManager { return success; } + public Boolean JoinFaction(ServerPlayerEntity player, String factionName) { + Boolean joinedFaction = factionConfig.factions.JoinFaction(factionName, player.getUuidAsString(), player.getEntityName()); + + if (joinedFaction) { + player.sendMessage(Text.of("Successfully joined the faction " + factionName)); + } else { + player.sendMessage(Text.of("Failed to join faction - you were not on the invite list!")); + } + + return joinedFaction; + } + ///////////////////////////////////////////////////////////////////////////// /// @fn ListOfFactions ///