From 4a93822854439424c7087102a059a807c0b620b4 Mon Sep 17 00:00:00 2001 From: Ray Neiheiser Date: Mon, 4 Nov 2024 22:13:22 +0100 Subject: [PATCH] Team fix (#10403) Calculate team via colony handler, no need to get colony for it --- src/main/java/com/minecolonies/api/colony/IColony.java | 6 +++--- .../api/entity/citizen/AbstractEntityCitizen.java | 4 ++-- .../citizen/citizenhandlers/ICitizenColonyHandler.java | 8 ++++++++ src/main/java/com/minecolonies/core/colony/Colony.java | 4 ++-- .../java/com/minecolonies/core/colony/ColonyView.java | 2 +- .../citizen/citizenhandlers/CitizenColonyHandler.java | 9 +++++++++ 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/minecolonies/api/colony/IColony.java b/src/main/java/com/minecolonies/api/colony/IColony.java index 2466d921c6c..b30f2377ad8 100755 --- a/src/main/java/com/minecolonies/api/colony/IColony.java +++ b/src/main/java/com/minecolonies/api/colony/IColony.java @@ -125,10 +125,10 @@ public interface IColony * * @return The team name */ - default String getTeamName() + static String getTeamName(final Level level, final int id) { - final String dim = getDimension().location().getPath(); - return TEAM_COLONY_NAME + "_" + (dim.length() > 10 ? dim.hashCode() : dim) + "_" + getID(); + final String dim = level.dimension().location().getPath(); + return TEAM_COLONY_NAME + "_" + (dim.length() > 10 ? dim.hashCode() : dim) + "_" + id; } /** diff --git a/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java b/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java index b0b9726e05b..4ca1ae4dc6f 100755 --- a/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java +++ b/src/main/java/com/minecolonies/api/entity/citizen/AbstractEntityCitizen.java @@ -245,11 +245,11 @@ public boolean isNoAi() protected PlayerTeam getAssignedTeam() { final ICitizenColonyHandler citizenColonyHandler = getCitizenColonyHandler(); - if (citizenColonyHandler == null || citizenColonyHandler.getColony() == null) + if (citizenColonyHandler == null) { return null; } - return citizenColonyHandler.getColony().getTeam(); + return citizenColonyHandler.getTeam(level); } /** diff --git a/src/main/java/com/minecolonies/api/entity/citizen/citizenhandlers/ICitizenColonyHandler.java b/src/main/java/com/minecolonies/api/entity/citizen/citizenhandlers/ICitizenColonyHandler.java index 9894600f529..122a60d4711 100755 --- a/src/main/java/com/minecolonies/api/entity/citizen/citizenhandlers/ICitizenColonyHandler.java +++ b/src/main/java/com/minecolonies/api/entity/citizen/citizenhandlers/ICitizenColonyHandler.java @@ -3,6 +3,8 @@ import com.minecolonies.api.colony.IColony; import com.minecolonies.api.colony.buildings.IBuilding; import net.minecraft.network.syncher.EntityDataAccessor; +import net.minecraft.world.level.Level; +import net.minecraft.world.scores.PlayerTeam; import org.jetbrains.annotations.Nullable; public interface ICitizenColonyHandler @@ -73,4 +75,10 @@ public interface ICitizenColonyHandler void onSyncDataUpdate(EntityDataAccessor dataAccessor); boolean registered(); + + /** + * Get the citizen team. + * @return the team. + */ + PlayerTeam getTeam(final Level level); } diff --git a/src/main/java/com/minecolonies/core/colony/Colony.java b/src/main/java/com/minecolonies/core/colony/Colony.java index 7f0ae40194d..33b4bb90e05 100644 --- a/src/main/java/com/minecolonies/core/colony/Colony.java +++ b/src/main/java/com/minecolonies/core/colony/Colony.java @@ -375,7 +375,7 @@ public class Colony implements IColony this.colonyFlag = new BannerPatternLayers.Builder().add(Utils.getRegistryValue(BannerPatterns.BASE, world), DyeColor.WHITE).build(); this.dimensionId = world.dimension(); onWorldLoad(world); - checkOrCreateTeam(world, getTeamName(), false); + checkOrCreateTeam(world, IColony.getTeamName(world, id), false); } colonyStateMachine = new TickRateStateMachine<>(INACTIVE, e -> @@ -640,7 +640,7 @@ public void updateAttackingPlayers() public PlayerTeam getTeam() { // This getter will create the team if it doesn't exist. Could do something different though in the future. - return checkOrCreateTeam(world, getTeamName(), false); + return checkOrCreateTeam(world, IColony.getTeamName(world, id), false); } /** diff --git a/src/main/java/com/minecolonies/core/colony/ColonyView.java b/src/main/java/com/minecolonies/core/colony/ColonyView.java index 55646a70eb7..efecf3b7817 100644 --- a/src/main/java/com/minecolonies/core/colony/ColonyView.java +++ b/src/main/java/com/minecolonies/core/colony/ColonyView.java @@ -1258,7 +1258,7 @@ public boolean isDay() @Override public PlayerTeam getTeam() { - return getWorld().getScoreboard().getPlayerTeam(getTeamName()); + return world.getScoreboard().getPlayerTeam(IColony.getTeamName(world, id)); } @Override diff --git a/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenColonyHandler.java b/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenColonyHandler.java index 0a83b1f6074..0b11c7d0afe 100755 --- a/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenColonyHandler.java +++ b/src/main/java/com/minecolonies/core/entity/citizen/citizenhandlers/CitizenColonyHandler.java @@ -9,10 +9,13 @@ import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.Level; +import net.minecraft.world.scores.PlayerTeam; import org.jetbrains.annotations.Nullable; import static com.minecolonies.api.entity.citizen.AbstractEntityCitizen.*; import static com.minecolonies.api.util.constant.CitizenConstants.SATURATION_DECREASE_FACTOR; +import static com.minecolonies.core.util.TeamUtils.checkOrCreateTeam; /** * Handles all colony related methods for the citizen. @@ -233,4 +236,10 @@ public void onCitizenRemoved() citizen.getCitizenData().setLastPosition(citizen.blockPosition()); } } + + @Override + public PlayerTeam getTeam(final Level level) + { + return checkOrCreateTeam(level, IColony.getTeamName(level, colonyId), false); + } }