Skip to content

Commit

Permalink
No more teams (#10439)
Browse files Browse the repository at this point in the history
Remove teams in favor of our system that prevents friendly fire and guarantees the colors.
Rename getColony method in colony handler to be more clear what it does and add a method that just does a simple fetch.
  • Loading branch information
Raycoms committed Nov 12, 2024
1 parent 098851d commit 376985a
Show file tree
Hide file tree
Showing 62 changed files with 256 additions and 386 deletions.
19 changes: 0 additions & 19 deletions src/main/java/com/minecolonies/api/colony/IColony.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.minecraft.world.level.block.entity.BannerPatternLayers;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.scores.PlayerTeam;
import net.neoforged.neoforge.event.tick.LevelTickEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -120,17 +119,6 @@ public interface IColony
*/
boolean hasBuilding(final String building, final int level, final boolean singleBuilding);

/**
* Defines the team name for all colonies (both Colony and ColonyView)
*
* @return The team name
*/
static String getTeamName(final Level level, final int id)
{
final String dim = level.dimension().location().getPath();
return TEAM_COLONY_NAME + "_" + (dim.length() > 10 ? dim.hashCode() : dim) + "_" + id;
}

/**
* Getter for the team colony color.
*
Expand All @@ -152,13 +140,6 @@ static String getTeamName(final Level level, final int id)
*/
boolean isDay();

/**
* Retrieves the team of the colony
*
* @return Team of the colony
*/
PlayerTeam getTeam();

/**
* Get the last contact of a player to the colony in hours.
*
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/minecolonies/api/colony/IColonyView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -334,9 +333,6 @@ public interface IColonyView extends IColony
@Override
boolean hasWarehouse();

@Override
PlayerTeam getTeam();

@Override
int getLastContactInHours();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import com.minecolonies.core.entity.pathfinding.navigation.AbstractAdvancedPathNavigate;
import com.minecolonies.core.entity.pathfinding.navigation.PathingStuckHandler;
import com.mojang.datafixers.util.Pair;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
Expand All @@ -52,7 +55,6 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.items.IItemHandler;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -240,18 +242,6 @@ public boolean isNoAi()
return false;
}

@Override
@Nullable
protected PlayerTeam getAssignedTeam()
{
final ICitizenColonyHandler citizenColonyHandler = getCitizenColonyHandler();
if (citizenColonyHandler == null)
{
return null;
}
return citizenColonyHandler.getTeam(level());
}

/**
* Sets the textures of all citizens and distinguishes between male and female.
*/
Expand Down Expand Up @@ -813,4 +803,29 @@ public IItemHandler getItemHandlerCap(final Direction facing)
final ICitizenData data = getCitizenData();
return data == null ? null : data.getInventory();
}

@Override
public int getTeamColor()
{
if (getCitizenColonyHandler().getColony() == null)
{
return super.getTeamColor();
}
return getCitizenColonyHandler().getColony().getTeamColonyColor().getColor();
}

@Override
@NotNull
public Component getDisplayName()
{
if (getCitizenColonyHandler().getColony() == null)
{
return super.getDisplayName();
}
if (getName() instanceof MutableComponent mutableComponent)
{
return mutableComponent.withStyle(getCitizenColonyHandler().getColony().getTeamColonyColor()).withStyle((style) -> style.withHoverEvent(this.createHoverEvent()).withInsertion(this.getStringUUID()));
}
return super.getDisplayName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
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
Expand Down Expand Up @@ -46,7 +44,7 @@ public interface ICitizenColonyHandler
* @return the colony of the citizen or null.
*/
@Nullable
IColony getColony();
IColony getColonyOrRegister();

/**
* Getter for the colony id.
Expand Down Expand Up @@ -77,8 +75,8 @@ public interface ICitizenColonyHandler
boolean registered();

/**
* Get the citizen team.
* @return the team.
* Unsafe colony getter, doesn't run registration.
* @return the colony.
*/
PlayerTeam getTeam(final Level level);
IColony getColony();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.portal.DimensionTransition;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
Expand All @@ -49,7 +48,6 @@
import static com.minecolonies.api.util.constant.ColonyManagerConstants.NO_COLONY_ID;
import static com.minecolonies.api.util.constant.NbtTagConstants.*;
import static com.minecolonies.api.util.constant.RaiderConstants.*;
import static com.minecolonies.core.util.TeamUtils.checkOrCreateTeam;

/**
* Abstract for all raider entities.
Expand Down Expand Up @@ -725,13 +723,6 @@ public void initStatsFor(final double baseHealth, final double difficulty, final
this.setHealth(this.getMaxHealth());
}

@Override
@Nullable
protected PlayerTeam getAssignedTeam()
{
return checkOrCreateTeam(level(), RAID_TEAM);
}

/**
* Get the mobs difficulty
*
Expand Down Expand Up @@ -767,4 +758,11 @@ public ITickRateStateMachine<IState> getAI()
{
return ai;
}

@Override
public int getTeamId()
{
// All raiders are in the same team. You're doomed!
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.minecolonies.api.util.constant.ColonyConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand All @@ -16,13 +17,10 @@
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.level.portal.DimensionTransition;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.scores.PlayerTeam;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Special abstract minecolonies mob that overrides laggy vanilla behaviour.
Expand Down Expand Up @@ -301,78 +299,6 @@ public void updateSwimAmount()

}

/**
* Get the team this entity is assigned to.
*
* @return the team instance.
*/
@Nullable
protected abstract PlayerTeam getAssignedTeam();

@Override
@Nullable
public final PlayerTeam getTeam()
{
final PlayerTeam assignedTeam = getAssignedTeam();
registerToTeamInternal(assignedTeam);
return assignedTeam;
}

/**
* Register this entity to its own assigned team.
*/
public void registerToTeam()
{
registerToTeamInternal(getAssignedTeam());
}

/**
* Internal method for team registration.
*
* @param team the team to register to.
*/
private void registerToTeamInternal(@Nullable final PlayerTeam team)
{
if (team != null && !isInTeam(team))
{
level().getScoreboard().addPlayerToTeam(getScoreboardName(), team);
}
}

/**
* Remove the entity from its own assigned team.
*/
public void removeFromTeam()
{
final PlayerTeam team = getAssignedTeam();
if (team != null && isInTeam(team))
{
level().getScoreboard().removePlayerFromTeam(getScoreboardName(), team);
}
}

/**
* Check if the current entity is assigned to the provided team.
*
* @param team the input team.
* @return true if so.
*/
private boolean isInTeam(@NotNull final PlayerTeam team)
{
return Objects.equals(level().getScoreboard().getPlayersTeam(getScoreboardName()), team);
}

@Override
public void remove(@NotNull final RemovalReason reason)
{
super.remove(reason);
final PlayerTeam playersTeam = level().getScoreboard().getPlayersTeam(getScoreboardName());
if (playersTeam != null)
{
level().getScoreboard().removePlayerFromTeam(getScoreboardName(), playersTeam);
}
}

/**
* Static Byte values to avoid frequent autoboxing
*/
Expand Down Expand Up @@ -407,4 +333,21 @@ public void knockback(double power, double xRatio, double zRatio)
super.knockback(power, xRatio, zRatio);
}
}

@Override
public boolean hurt(final DamageSource dmgSource, final float dmg)
{
if (dmgSource.getEntity() instanceof AbstractFastMinecoloniesEntity otherFastMinecolEntity && otherFastMinecolEntity.getTeamId() == getTeamId())
{
return false;
}
return super.hurt(dmgSource, dmg);
}

/**
* Get the team name of this entity.
* todo sam make colony ids unique across dimensions.
* @return the team name.
*/
public abstract int getTeamId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private Optional<Boolean> test(@NotNull final LootContext lootContext, @Nullable
{
return Optional.ofNullable(entity)
.map(e -> e instanceof AbstractEntityCitizen citizen ? citizen : null)
.flatMap(c -> test(lootContext, c.getCitizenColonyHandler().getColony()));
.flatMap(c -> test(lootContext, c.getCitizenColonyHandler().getColonyOrRegister()));
}

private Optional<Boolean> test(@NotNull final LootContext lootContext, @Nullable Vec3 origin)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/minecolonies/api/util/FoodUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static double getFoodValue(final ItemStack foodStack, final AbstractEntit
{
final FoodProperties itemFood = foodStack.getItem().getFoodProperties(foodStack, citizen);
final int housingLevel = citizen.getCitizenData().getHomeBuilding() == null ? 0 : citizen.getCitizenData().getHomeBuilding().getBuildingLevel();
final double researchBonus = citizen.getCitizenColonyHandler().getColony().getResearchManager().getResearchEffects().getEffectStrength(SATURATION);
final double researchBonus = citizen.getCitizenColonyHandler().getColonyOrRegister().getResearchManager().getResearchEffects().getEffectStrength(SATURATION);
return getFoodValue(foodStack, itemFood, housingLevel, researchBonus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ public static void consumeFood(final ItemStack foodStack, final AbstractEntityCi
}
}

IColony citizenColony = citizen.getCitizenColonyHandler().getColony();
IColony citizenColony = citizen.getCitizenColonyHandler().getColonyOrRegister();
if (citizenColony != null)
{
AdvancementUtils.TriggerAdvancementPlayersForColony(citizenColony, playerMP -> AdvancementTriggers.CITIZEN_EAT_FOOD.get().trigger(playerMP, foodStack));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/minecolonies/api/util/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static MessageBuilder forCitizen(final AbstractEntityCitizen citizen, fin
*/
public static MessageBuilder forCitizen(final AbstractEntityCitizen citizen, Component component)
{
if (citizen.getCitizenColonyHandler().getColony() != null)
if (citizen.getCitizenColonyHandler().getColonyOrRegister() != null)
{
final IJob<?> job = citizen.getCitizenJobHandler().getColonyJob();

Expand Down
Loading

0 comments on commit 376985a

Please sign in to comment.