Properly set players' race when joining. Should fix players getting stuck as humans when joining a server they played on before the mod was installed.

This commit is contained in:
Sollace 2023-01-21 00:28:59 +00:00
parent fef8b05fea
commit 5f1e6c24f0
14 changed files with 31 additions and 40 deletions

View file

@ -23,7 +23,7 @@ import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
public final class Race implements Affine {
public static final String DEFAULT_ID = "unicopia:human";
public static final String DEFAULT_ID = "unicopia:unset";
public static final Registry<Race> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("race"), DEFAULT_ID);
public static final RegistryKey<? extends Registry<Race>> REGISTRY_KEY = REGISTRY.getKey();
private static final DynamicCommandExceptionType UNKNOWN_RACE_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("race.unknown", id));
@ -44,6 +44,7 @@ public final class Race implements Affine {
* The default, unset race.
* This is used if there are no other races.
*/
public static final Race UNSET = register("unset", false, FlightType.NONE, false);
public static final Race HUMAN = register("human", false, FlightType.NONE, false);
public static final Race EARTH = register("earth", false, FlightType.NONE, true);
public static final Race UNICORN = register("unicorn", true, FlightType.NONE, false);
@ -70,15 +71,19 @@ public final class Race implements Affine {
}
public boolean hasIronGut() {
return isUsable() && this != CHANGELING;
return !isHuman() && this != CHANGELING;
}
public boolean isUsable() {
return !isDefault();
public boolean isUnset() {
return this == UNSET;
}
public boolean isDefault() {
return this == HUMAN;
public boolean isEquine() {
return !isHuman();
}
public boolean isHuman() {
return this == UNSET || this == HUMAN;
}
public boolean isOp() {
@ -135,7 +140,7 @@ public final class Race implements Affine {
Set<String> whitelist = Unicopia.getConfig().speciesWhiteList.get();
return isDefault()
return isUnset()
|| whitelist.isEmpty()
|| whitelist.contains(getId().toString());
}

View file

@ -58,7 +58,7 @@ public class EarthPonyKickAbility implements Ability<Pos> {
@Override
public double getCostEstimate(Pony player) {
double distance = MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isDefault() ? 6 : -6;
double distance = MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isHuman() ? 6 : -6;
return TraceHelper.findBlock(player.asEntity(), distance, 1)
.filter(pos -> TreeType.at(pos, player.asWorld()) != TreeType.NONE)
@ -113,13 +113,13 @@ public class EarthPonyKickAbility implements Ability<Pos> {
}
private int getKickDirection(Pony player) {
return MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isDefault() ? 1 : -1;
return MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isHuman() ? 1 : -1;
}
private Pos getDefaultKickLocation(Pony player) {
Vec3d kickVector = player.asEntity().getRotationVector().multiply(1, 0, 1);
if (!MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isDefault()) {
if (MineLPDelegate.getInstance().getPlayerPonyRace(player.asEntity()).isEquine()) {
kickVector = kickVector.rotateY((float)Math.PI);
}
return new Pos(new BlockPos(player.getOriginVector().add(kickVector)));

View file

@ -47,7 +47,7 @@ public class UnicopiaClient implements ClientModInitializer {
&& MinecraftClient.getInstance().player != null) {
Race race = MineLPDelegate.getInstance().getPlayerPonyRace();
if (!race.isDefault()) {
if (race.isEquine()) {
return race;
}
}

View file

@ -89,7 +89,7 @@ public class LanSettingsScreen extends GameGui {
if (whitelist.isEmpty() && forceShowWhitelist) {
for (Race r : Race.REGISTRY) {
if (!r.isDefault()) {
if (!r.isUnset()) {
whitelist.add(r.getId().toString());
}
}
@ -113,7 +113,7 @@ public class LanSettingsScreen extends GameGui {
WHITELIST_GRID_PACKER.start();
for (Race race : Race.REGISTRY) {
if (!race.isDefault()) {
if (!race.isUnset()) {
Bounds bound = WHITELIST_GRID_PACKER.next();
Button button = content.addButton(new Toggle(LEFT + bound.left + 10, row + bound.top, whitelist.contains(race.getId().toString())))

View file

@ -49,7 +49,7 @@ public class TribeSelectionScreen extends GameGui implements HidesHud {
final int itemWidth = 70 + 10;
List<Race> options = Race.REGISTRY.stream().filter(race -> !race.isDefault() && !race.isOp()).toList();
List<Race> options = Race.REGISTRY.stream().filter(race -> !race.isHuman() && !race.isOp()).toList();
int columns = Math.min(width / itemWidth, options.size());

View file

@ -8,7 +8,6 @@ import com.minelittlepony.unicopia.client.FirstPersonRendererOverrides.ArmRender
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.feature.FeatureRenderer;
import net.minecraft.client.render.entity.feature.FeatureRendererContext;

View file

@ -91,7 +91,7 @@ public class BraceletFeatureRenderer<E extends LivingEntity> implements Accessor
boolean glowing = ((GlowableItem)item.getItem()).isGlowing(item);
if (!MineLPDelegate.getInstance().getPlayerPonyRace((ClientPlayerEntity)entity).isDefault()) {
if (MineLPDelegate.getInstance().getPlayerPonyRace((ClientPlayerEntity)entity).isEquine()) {
stack.translate(side == Arm.LEFT ? 0.06 : -0.06, 0.3, 0);
} else {
stack.translate(0, -0.1, 0);

View file

@ -20,11 +20,7 @@ import net.minecraft.util.Arm;
import net.minecraft.util.math.*;
public class HeldEntityFeatureRenderer<E extends LivingEntity> implements AccessoryFeatureRenderer.Feature<E> {
private final FeatureRendererContext<E, ? extends BipedEntityModel<E>> context;
public HeldEntityFeatureRenderer(FeatureRendererContext<E, ? extends BipedEntityModel<E>> context) {
this.context = context;
}
@Override
@ -106,7 +102,7 @@ public class HeldEntityFeatureRenderer<E extends LivingEntity> implements Access
float passengerHeight = passenger.asEntity().getHeight() / 2F;
float carrierHeight = entity.asEntity().getHeight() / 5F;
if (entity instanceof Pony pony && !MineLPDelegate.getInstance().getPlayerPonyRace(pony.asEntity()).isDefault() && pony.getPhysics().isFlying()) {
if (entity instanceof Pony pony && MineLPDelegate.getInstance().getPlayerPonyRace(pony.asEntity()).isEquine() && pony.getPhysics().isFlying()) {
return new Vec3d(0,
-passenger.asEntity().getHeight() - passengerHeight,
0

View file

@ -29,7 +29,7 @@ public class PlayerPoser {
Pony pony = Pony.of(player);
float progress = pony.getAnimationProgress(MinecraftClient.getInstance().getTickDelta());
Animation animation = pony.getAnimation();
boolean isPony = !MineLPDelegate.getInstance().getPlayerPonyRace(player).isDefault();
boolean isPony = MineLPDelegate.getInstance().getPlayerPonyRace(player).isEquine();
ItemStack glasses = GlassesItem.getForEntity(player);

View file

@ -89,7 +89,7 @@ class SpeciesCommand {
boolean first = true;
for (Race i : Race.REGISTRY) {
if (!i.isDefault() && i.isPermitted(player)) {
if (!i.isUnset() && i.isPermitted(player)) {
message.append(Text.literal((!first ? "\n" : "") + " - "));
message.append(i.getDisplayName());
first = false;

View file

@ -303,7 +303,7 @@ public class PlayerPhysics extends EntityPhysics<PlayerEntity> implements Tickab
entity.setVelocity(velocity.toImmutable());
if (isFlying() && !entity.isFallFlying() && !pony.isHanging() && pony.isClient()) {
if (MineLPDelegate.getInstance().getPlayerPonyRace(entity).isDefault() && getHorizontalMotion() > 0.03) {
if (MineLPDelegate.getInstance().getPlayerPonyRace(entity).isEquine() && getHorizontalMotion() > 0.03) {
float pitch = ((LivingEntityDuck)entity).getLeaningPitch();
if (pitch < 1) {
if (pitch < 0.9F) {

View file

@ -82,7 +82,6 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
private final Interpolator interpolator = new LinearInterpolator();
private boolean dirty;
private boolean speciesPersisted;
private int ticksHanging;
@ -213,10 +212,6 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
return corruption;
}
public boolean isSpeciesPersisted() {
return speciesPersisted;
}
public boolean isSunImmune() {
return ticksSunImmunity > 0;
}
@ -602,7 +597,6 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
@Override
public void fromSynchronizedNbt(NbtCompound compound) {
super.fromSynchronizedNbt(compound);
speciesPersisted = true;
setSpecies(Race.fromName(compound.getString("playerSpecies"), Race.HUMAN));
powers.fromNBT(compound.getCompound("powers"));
gravity.fromNBT(compound.getCompound("gravity"));
@ -627,7 +621,6 @@ public class Pony extends Living<PlayerEntity> implements Copyable<Pony>, Update
@Override
public void copyFrom(Pony oldPlayer) {
speciesPersisted = true;
if (!oldPlayer.asEntity().isRemoved()) {
oldPlayer.getSpellSlot().stream(true).forEach(getSpellSlot()::put);
} else {

View file

@ -33,16 +33,16 @@ public interface Channel {
static void bootstrap() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
Pony pony = Pony.of(handler.player);
if (!pony.isSpeciesPersisted()) {
if (pony.getActualSpecies() == Race.UNSET) {
Race race = WorldTribeManager.forWorld(handler.player.getWorld()).getDefaultRace();
if (!race.isPermitted(handler.player)) {
race = Race.HUMAN;
race = Race.UNSET;
}
if (race.isUsable()) {
if (race.isUnset()) {
sender.sendPacket(SERVER_SELECT_TRIBE.id(), new MsgTribeSelect(Race.allPermitted(handler.player)).toBuffer());
} else {
pony.setSpecies(race);
Unicopia.LOGGER.info("Setting {}'s race to {} due to host setting", handler.player.getDisplayName().getString(), Race.REGISTRY.getId(race).toString());
} else {
sender.sendPacket(SERVER_SELECT_TRIBE.id(), new MsgTribeSelect(Race.allPermitted(handler.player)).toBuffer());
}
}
sender.sendPacket(SERVER_RESOURCES_SEND.id(), new MsgServerResources().toBuffer());

View file

@ -33,10 +33,8 @@ public record MsgRequestSpeciesChange (
public void handle(ServerPlayerEntity sender) {
Pony player = Pony.of(sender);
Race worldDefaultRace = WorldTribeManager.forWorld((ServerWorld)player.asWorld()).getDefaultRace();
if (force || player.getActualSpecies().isDefault() || (player.getActualSpecies() == worldDefaultRace && !player.isSpeciesPersisted())) {
player.setSpecies(newRace.isPermitted(sender) ? newRace : worldDefaultRace);
if (force || player.getActualSpecies().isUnset()) {
player.setSpecies(newRace.isPermitted(sender) ? newRace : WorldTribeManager.forWorld((ServerWorld)player.asWorld()).getDefaultRace());
if (force) {
player.onSpawn();