|
9 | 9 | import com.badlogic.gdx.math.Vector2; |
10 | 10 | import com.badlogic.gdx.math.Vector3; |
11 | 11 | import java.awt.Point; |
| 12 | +import java.util.HashSet; |
12 | 13 | import java.util.List; |
13 | 14 | import java.util.Map; |
14 | 15 | import java.util.Set; |
@@ -414,11 +415,30 @@ public boolean placeFenceSegment(int gridX, int gridY) { |
414 | 415 |
|
415 | 416 | try { |
416 | 417 | System.out.println("[FenceBuildingManager] Attempting to place fence at grid (" + gridPos.x + ", " + gridPos.y + ") with material " + selectedMaterialType + " for owner " + playerId); |
| 418 | + |
| 419 | + // Generate fence ID for both local and network use |
| 420 | + String fenceId = java.util.UUID.randomUUID().toString(); |
| 421 | + |
417 | 422 | // Attempt to place the fence piece with automatic piece type selection |
418 | | - FencePiece placedPiece = structureManager.addFencePiece(gridPos, selectedMaterialType, playerId); |
| 423 | + FencePiece placedPiece = structureManager.addFencePiece(gridPos, selectedMaterialType, playerId, fenceId); |
419 | 424 |
|
420 | 425 | if (placedPiece != null) { |
421 | | - System.out.println("[FenceBuildingManager] Fence piece created: " + placedPiece.getType() + " at world (" + placedPiece.getX() + ", " + placedPiece.getY() + ")"); |
| 426 | + System.out.println("[FenceBuildingManager] Fence piece created: " + placedPiece.getType() + " at world (" + placedPiece.getX() + ", " + placedPiece.getY() + ") with ID " + fenceId); |
| 427 | + |
| 428 | + // Send fence place message to server in multiplayer mode |
| 429 | + wagemaker.uk.network.GameClient gameClient = player.getGameClient(); |
| 430 | + System.out.println("DEBUG: Sending fence place message - gameClient=" + (gameClient != null ? "exists" : "null") + |
| 431 | + ", connected=" + (gameClient != null ? gameClient.isConnected() : "N/A")); |
| 432 | + if (gameClient != null && gameClient.isConnected()) { |
| 433 | + wagemaker.uk.network.FencePlaceMessage fenceMsg = new wagemaker.uk.network.FencePlaceMessage( |
| 434 | + playerId, fenceId, gridX, gridY, placedPiece.getType(), selectedMaterialType, playerId |
| 435 | + ); |
| 436 | + gameClient.sendMessage(fenceMsg); |
| 437 | + System.out.println("DEBUG: Sent FencePlaceMessage to server: " + fenceMsg); |
| 438 | + } else { |
| 439 | + System.out.println("DEBUG: NOT sending fence message - gameClient null or not connected"); |
| 440 | + } |
| 441 | + |
422 | 442 | // Consume materials from inventory |
423 | 443 | FenceMaterialProvider materialProvider = validator.getMaterialProvider(); |
424 | 444 | if (materialProvider != null) { |
@@ -555,6 +575,28 @@ public boolean removeFenceSegment(int gridX, int gridY) { |
555 | 575 | FencePiece removedPiece = structureManager.removeFencePiece(gridPos); |
556 | 576 |
|
557 | 577 | if (removedPiece != null) { |
| 578 | + // Get the fence ID from the removed piece |
| 579 | + String fenceId = removedPiece.getFenceId(); |
| 580 | + if (fenceId == null) { |
| 581 | + // Fallback for pieces without IDs (shouldn't happen in multiplayer) |
| 582 | + fenceId = "unknown-" + System.currentTimeMillis(); |
| 583 | + System.out.println("WARNING: Removed fence piece had no ID, using fallback: " + fenceId); |
| 584 | + } |
| 585 | + |
| 586 | + // Send fence remove message to server in multiplayer mode |
| 587 | + wagemaker.uk.network.GameClient gameClient = player.getGameClient(); |
| 588 | + System.out.println("DEBUG: Sending fence remove message - gameClient=" + (gameClient != null ? "exists" : "null") + |
| 589 | + ", connected=" + (gameClient != null ? gameClient.isConnected() : "N/A")); |
| 590 | + if (gameClient != null && gameClient.isConnected()) { |
| 591 | + wagemaker.uk.network.FenceRemoveMessage fenceMsg = new wagemaker.uk.network.FenceRemoveMessage( |
| 592 | + playerId, fenceId, gridX, gridY, materialToReturn, playerId |
| 593 | + ); |
| 594 | + gameClient.sendMessage(fenceMsg); |
| 595 | + System.out.println("DEBUG: Sent FenceRemoveMessage to server with fence ID: " + fenceId); |
| 596 | + } else { |
| 597 | + System.out.println("DEBUG: NOT sending fence remove message - gameClient null or not connected"); |
| 598 | + } |
| 599 | + |
558 | 600 | // Return materials to inventory |
559 | 601 | FenceMaterialProvider materialProvider = validator.getMaterialProvider(); |
560 | 602 | if (materialProvider != null) { |
@@ -922,20 +964,65 @@ public void clearNearestEnclosure() { |
922 | 964 | } |
923 | 965 |
|
924 | 966 | if (closestPos != null) { |
| 967 | + // Get the current player's ID for ownership validation |
| 968 | + String playerId = player.getPlayerId(); |
| 969 | + if (playerId == null) { |
| 970 | + playerId = "local_player"; |
| 971 | + } |
| 972 | + |
| 973 | + // Check if the closest piece is owned by the current player |
| 974 | + FencePiece closestPiece = structureManager.getFencePiece(closestPos); |
| 975 | + if (closestPiece == null || !playerId.equals(closestPiece.getOwnerId())) { |
| 976 | + System.out.println("[FenceBuildingManager] Cannot clear enclosure - closest fence piece is not owned by current player"); |
| 977 | + return; |
| 978 | + } |
| 979 | + |
925 | 980 | // Get connected pieces (the enclosure) |
926 | 981 | Set<Point> connectedPoints = structureManager.findConnectedPieces(closestPos); |
927 | 982 |
|
928 | 983 | if (!connectedPoints.isEmpty()) { |
929 | | - System.out.println("[FenceBuildingManager] Clearing enclosure of " + connectedPoints.size() + " pieces nearest to " + closestPos); |
| 984 | + // Filter to only include pieces owned by the current player |
| 985 | + Set<Point> ownedPieces = new HashSet<>(); |
| 986 | + for (Point pos : connectedPoints) { |
| 987 | + FencePiece piece = structureManager.getFencePiece(pos); |
| 988 | + if (piece != null && playerId.equals(piece.getOwnerId())) { |
| 989 | + ownedPieces.add(pos); |
| 990 | + } |
| 991 | + } |
| 992 | + |
| 993 | + if (ownedPieces.isEmpty()) { |
| 994 | + System.out.println("[FenceBuildingManager] Cannot clear enclosure - no pieces owned by current player"); |
| 995 | + return; |
| 996 | + } |
| 997 | + |
| 998 | + System.out.println("[FenceBuildingManager] Clearing " + ownedPieces.size() + " owned pieces out of " + connectedPoints.size() + " total pieces nearest to " + closestPos); |
930 | 999 |
|
931 | 1000 | int removedCount = 0; |
932 | 1001 |
|
933 | | - // Remove each piece |
934 | | - for (Point pos : connectedPoints) { |
| 1002 | + // Remove each owned piece and send network messages |
| 1003 | + for (Point pos : ownedPieces) { |
935 | 1004 | FencePiece removedPiece = structureManager.removeFencePiece(pos); |
936 | 1005 | if (removedPiece != null) { |
937 | 1006 | updateCollisionBoundaries(pos, false); |
938 | 1007 | removedCount++; |
| 1008 | + |
| 1009 | + // Send fence remove message to server in multiplayer mode |
| 1010 | + |
| 1011 | + wagemaker.uk.network.GameClient gameClient = player.getGameClient(); |
| 1012 | + if (gameClient != null && gameClient.isConnected()) { |
| 1013 | + String fenceId = removedPiece.getFenceId(); |
| 1014 | + if (fenceId == null) { |
| 1015 | + fenceId = "unknown-" + System.currentTimeMillis() + "-" + pos.x + "-" + pos.y; |
| 1016 | + System.out.println("WARNING: Removed fence piece had no ID, using fallback: " + fenceId); |
| 1017 | + } |
| 1018 | + |
| 1019 | + wagemaker.uk.network.FenceRemoveMessage fenceMsg = new wagemaker.uk.network.FenceRemoveMessage( |
| 1020 | + playerId, fenceId, pos.x, pos.y, selectedMaterialType, playerId |
| 1021 | + ); |
| 1022 | + gameClient.sendMessage(fenceMsg); |
| 1023 | + System.out.println("DEBUG: Sent FenceRemoveMessage for enclosure clear - fence ID: " + fenceId + " at (" + pos.x + ", " + pos.y + ")"); |
| 1024 | + } |
| 1025 | + |
939 | 1026 | // Trigger visual effect for each piece |
940 | 1027 | if (visualEffectsManager != null) { |
941 | 1028 | visualEffectsManager.triggerRemovalAnimation(pos, selectedMaterialType); |
|
0 commit comments