Skip to content

Commit

Permalink
Fixed some UI issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Feb 8, 2024
1 parent 82a056e commit c1840a7
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 82 deletions.
6 changes: 6 additions & 0 deletions src/api/java/mods/railcraft/api/core/CompoundTagKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public final class CompoundTagKeys {
public static final String DECIDING_CARTS = "decidingCarts";
public static final String DEFAULT_ASPECT = "defaultAspect";
public static final String DELAY = "delay";
public static final String DEST = "dest";
public static final String EMITTER_POS = "emitterPos";
public static final String ENERGY = "energy";
public static final String ENTRIES = "entries";
public static final String FACING = "facing";
public static final String FEED_COUNTER = "feedCounter";
public static final String FILTER = "filter";
public static final String FLIPPED = "flipped";
public static final String FUEL = "fuel";
public static final String ID = "id";
public static final String INPUT_SIGNAL = "inputSignal";
Expand Down Expand Up @@ -67,18 +69,22 @@ public final class CompoundTagKeys {
public static final String POWERED = "powered";
public static final String POWERED_ASPECT = "poweredAspect";
public static final String PREV_CART_ID = "prevCartId";
public static final String PRIMARY_COLOR = "primaryColor";
public static final String PROCESSING = "processing";
public static final String PROCESS_STATE = "processState";
public static final String PROGRESS = "progress";
public static final String RAILWAY = "railway";
public static final String REBUILD_DELAY = "rebuildDelay";
public static final String REDSTONE_TRIGGERED = "redstoneTriggered";
public static final String REVERSE = "reverse";
public static final String ROTOR_CONTAINER = "rotorContainer";
public static final String SECONDARY_COLOR = "secondaryColor";
public static final String SIGNALS = "signals";
public static final String SIGNAL_ASPECT = "signalAspect";
public static final String SIGNAL_ASPECT_TRIGGER_SIGNALS = "signalAspectTriggerSignals";
public static final String SIGNAL_CONTROLLER = "signalController";
public static final String SIGNAL_RECEIVER = "signalReceiver";
public static final String SPEED = "speed";
public static final String SPRINGING_CARTS = "springingCarts";
public static final String SPRUNG = "sprung";
public static final String STATE = "state";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.commons.lang3.StringUtils;
import mods.railcraft.Translations;
import mods.railcraft.client.gui.widget.button.ButtonTexture;
import mods.railcraft.client.gui.widget.button.MultiButton;
Expand Down Expand Up @@ -83,22 +82,15 @@ public void init() {

// Reverse button
this.reverseButton = this.addRenderableWidget(ToggleButton
.toggleBuilder(
Component.literal("R"),
__ -> this.toggleReverse(),
ButtonTexture.SMALL_BUTTON)
.toggleBuilder(Component.literal("R"), __ -> this.toggleReverse(), ButtonTexture.SMALL_BUTTON)
.bounds(centreX + 4, centreY + this.getYSize() - 112, 12, 16)
.toggled(this.locomotive.isReverse())
.build());

// Speed buttons
for (var speed : Speed.values()) {
var label =
IntStream.range(0, speed.getLevel()).mapToObj(i -> ">").collect(Collectors.joining());

var button = RailcraftButton
.builder(
Component.literal(label),
.builder(Component.literal(StringUtils.repeat('>', speed.getLevel())),
__ -> this.setSpeed(speed),
ButtonTexture.SMALL_BUTTON)
.pos(0, centreY + this.getYSize() - 112)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public void setState(T state) {
if (this.state != state && this.stateCallback != null) {
this.state = state;
this.stateCallback.accept(this.state);
this.setTexturePosition(this.state.texturePosition());
this.setMessage(this.state.label());
this.setTooltip(this.tooltipFactory.createTooltip(this.state).orElse(null));
}
this.setMessage(this.state.label());
this.setTooltip(this.tooltipFactory.createTooltip(this.state).orElse(null));
this.setTexturePosition(this.state.texturePosition());
}

public static <T extends ButtonState<T>> Builder<T> builder(
Expand Down
22 changes: 2 additions & 20 deletions src/main/java/mods/railcraft/network/RailcraftDataSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,8 @@ public byte[] copy(byte[] value) {
};

public static final EntityDataSerializer<Optional<GameProfile>> OPTIONAL_GAME_PROFILE =
new EntityDataSerializer<>() {
@Override
public void write(FriendlyByteBuf packetBuffer, Optional<GameProfile> gameProfile) {
packetBuffer.writeOptional(gameProfile, FriendlyByteBuf::writeGameProfile);
}

@Override
public Optional<GameProfile> read(FriendlyByteBuf packetBuffer) {
return packetBuffer.readOptional(FriendlyByteBuf::readGameProfile);
}

@Override
public Optional<GameProfile> copy(Optional<GameProfile> gameProfile) {
if (gameProfile.isEmpty()) {
return Optional.empty();
}
var profile = gameProfile.get();
return Optional.of(new GameProfile(profile.getId(), profile.getName()));
}
};
EntityDataSerializer
.optional(FriendlyByteBuf::writeGameProfile, FriendlyByteBuf::readGameProfile);

private static final DeferredRegister<EntityDataSerializer<?>> deferredRegister =
DeferredRegister.create(ForgeRegistries.Keys.ENTITY_DATA_SERIALIZERS, RailcraftConstants.ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import mods.railcraft.Translations;
import mods.railcraft.api.carts.RollingStock;
import mods.railcraft.api.core.CompoundTagKeys;
import mods.railcraft.api.track.TrackUtil;
import mods.railcraft.api.util.EnumUtil;
import mods.railcraft.client.gui.widget.button.ButtonTexture;
Expand Down Expand Up @@ -115,7 +116,7 @@ protected void addAdditionalSaveData(CompoundTag tag) {
@Override
protected void readAdditionalSaveData(CompoundTag tag) {
super.readAdditionalSaveData(tag);
this.setMode(Mode.fromName(tag.getString("mode")));
this.setMode(Mode.fromName(tag.getString(CompoundTagKeys.MODE)));
}

protected boolean placeNewTrack(BlockPos pos, int slotStock, RailShape railShape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import net.minecraftforge.network.NetworkHooks;

/**
* Basetype of RC minecarts. It also contains some generic code that most carts will find useful.
* Base type of RC minecarts. It also contains some generic code that most carts will find useful.
*/
public abstract class RailcraftMinecart extends AbstractMinecartContainer
implements SeasonalCart, ItemTransferHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected DyeColor getDefaultSecondaryColor() {
}

protected void loadFromItemStack(ItemStack itemStack) {
CompoundTag tag = itemStack.getTag();
var tag = itemStack.getTag();
if (tag == null || !(itemStack.getItem() instanceof LocomotiveItem)) {
return;
}
Expand All @@ -149,7 +149,7 @@ protected void loadFromItemStack(ItemStack itemStack) {
}

if (tag.contains(CompoundTagKeys.OWNER, Tag.TAG_COMPOUND)) {
GameProfile ownerProfile = NbtUtils.readGameProfile(tag.getCompound(CompoundTagKeys.OWNER));
var ownerProfile = NbtUtils.readGameProfile(tag.getCompound(CompoundTagKeys.OWNER));
this.setOwner(ownerProfile);
this.setLock(Lock.LOCKED);
}
Expand Down Expand Up @@ -213,23 +213,23 @@ public ItemStack getPickResult() {
@Override
public InteractionResult interact(Player player, InteractionHand hand) {
if (this.level().isClientSide()) {
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(this.level().isClientSide());
}

ItemStack itemStack = player.getItemInHand(hand);
var itemStack = player.getItemInHand(hand);
if (!itemStack.isEmpty() && itemStack.is(RailcraftItems.WHISTLE_TUNER.get())) {
if (this.whistleDelay <= 0) {
this.whistlePitch = this.getNewWhistlePitch();
this.whistle();
itemStack.hurtAndBreak(1, (ServerPlayer) player,
serverPlayerEntity -> player.broadcastBreakEvent(hand));
}
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(this.level().isClientSide());
}
if (this.canControl(player)) {
super.interact(player, hand);
return super.interact(player, hand);
}
return InteractionResult.CONSUME;
return InteractionResult.sidedSuccess(this.level().isClientSide());
}

/**
Expand Down Expand Up @@ -607,10 +607,10 @@ protected void updateFuel() {

protected abstract int retrieveFuel();

public int getDamageToRoadKill(LivingEntity entity) {
private int getDamageToRoadKill(LivingEntity entity) {
if (entity instanceof Player) {
ItemStack pants = entity.getItemBySlot(EquipmentSlot.LEGS);
if (RailcraftItems.OVERALLS.get() == pants.getItem()) {
var pants = entity.getItemBySlot(EquipmentSlot.LEGS);
if (pants.is(RailcraftItems.OVERALLS.get())) {
pants.hurtAndBreak(5, entity,
unusedThing -> entity.broadcastBreakEvent(EquipmentSlot.LEGS));
return 4;
Expand Down Expand Up @@ -702,54 +702,56 @@ public double getDrag() {
public void addAdditionalSaveData(CompoundTag tag) {
super.addAdditionalSaveData(tag);

tag.putBoolean("flipped", this.flipped);
tag.putBoolean(CompoundTagKeys.FLIPPED, this.flipped);

tag.putString("dest", StringUtils.defaultIfBlank(getDestination(), ""));
tag.putString(CompoundTagKeys.DEST, StringUtils.defaultIfBlank(getDestination(), ""));

tag.putString("mode", this.getMode().getSerializedName());
tag.putString("speed", this.getSpeed().getSerializedName());
tag.putString("lock", this.getLock().getSerializedName());
tag.putString(CompoundTagKeys.MODE, this.getMode().getSerializedName());
tag.putString(CompoundTagKeys.SPEED, this.getSpeed().getSerializedName());
tag.putString(CompoundTagKeys.LOCK, this.getLock().getSerializedName());

tag.putString("primaryColor",
tag.putString(CompoundTagKeys.PRIMARY_COLOR,
DyeColor.byId(this.entityData.get(PRIMARY_COLOR)).getSerializedName());
tag.putString("secondaryColor",
tag.putString(CompoundTagKeys.SECONDARY_COLOR,
DyeColor.byId(this.entityData.get(SECONDARY_COLOR)).getSerializedName());

tag.putFloat("whistlePitch", this.whistlePitch);
tag.putFloat(CompoundTagKeys.WHISTLE_PITCH, this.whistlePitch);

tag.putInt("fuel", this.fuel);
tag.putInt(CompoundTagKeys.FUEL, this.fuel);

tag.putBoolean("reverse", this.isReverse());
this.getOwner()
.ifPresent(owner -> tag.put("owner", NbtUtils.writeGameProfile(new CompoundTag(), owner)));
tag.putBoolean(CompoundTagKeys.REVERSE, this.isReverse());
this.getOwner().ifPresent(owner ->
tag.put(CompoundTagKeys.OWNER, NbtUtils.writeGameProfile(new CompoundTag(), owner)));
}

@Override
public void readAdditionalSaveData(CompoundTag tag) {
super.readAdditionalSaveData(tag);

this.flipped = tag.getBoolean("flipped");
this.flipped = tag.getBoolean(CompoundTagKeys.FLIPPED);

this.setDestination(tag.getString("dest"));
this.setDestination(tag.getString(CompoundTagKeys.DEST));

this.setMode(Mode.fromName(tag.getString("mode")));
this.setSpeed(Speed.fromName(tag.getString("speed")));
this.setLock(Lock.fromName(tag.getString("lock")));
this.setMode(Mode.fromName(tag.getString(CompoundTagKeys.MODE)));
this.setSpeed(Speed.fromName(tag.getString(CompoundTagKeys.SPEED)));
this.setLock(Lock.fromName(tag.getString(CompoundTagKeys.LOCK)));

this.setPrimaryColor(
DyeColor.byName(tag.getString("primaryColor"), this.getDefaultPrimaryColor()));
DyeColor.byName(tag.getString(CompoundTagKeys.PRIMARY_COLOR), this.getDefaultPrimaryColor()));
this.setSecondaryColor(
DyeColor.byName(tag.getString("secondaryColor"), this.getDefaultSecondaryColor()));
DyeColor.byName(tag.getString(CompoundTagKeys.SECONDARY_COLOR), this.getDefaultSecondaryColor()));

this.whistlePitch = tag.getFloat("whistlePitch");
this.whistlePitch = tag.getFloat(CompoundTagKeys.WHISTLE_PITCH);

this.fuel = tag.getInt("fuel");
this.fuel = tag.getInt(CompoundTagKeys.FUEL);

if (tag.contains("reverse", Tag.TAG_BYTE)) {
this.getEntityData().set(REVERSE, tag.getBoolean("reverse"));
if (tag.contains(CompoundTagKeys.REVERSE, Tag.TAG_BYTE)) {
this.getEntityData().set(REVERSE, tag.getBoolean(CompoundTagKeys.REVERSE));
}
if (tag.contains("owner", Tag.TAG_COMPOUND)) {
this.setOwner(NbtUtils.readGameProfile(tag.getCompound("owner")));;
if (tag.contains(CompoundTagKeys.OWNER, Tag.TAG_COMPOUND)) {
this.setOwner(NbtUtils.readGameProfile(tag.getCompound(CompoundTagKeys.OWNER)));;
} else {
this.setOwner(null);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/mods/railcraft/world/item/LocomotiveItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;
import com.mojang.authlib.GameProfile;
import mods.railcraft.Translations;
import mods.railcraft.api.core.CompoundTagKeys;
import mods.railcraft.api.item.Filter;
import mods.railcraft.api.item.MinecartFactory;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -62,20 +63,20 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
public static void setItemColorData(ItemStack stack, DyeColor primaryColor,
DyeColor secondaryColor) {
var tag = stack.getOrCreateTag();
tag.putInt("primaryColor", primaryColor.getId());
tag.putInt("secondaryColor", secondaryColor.getId());
tag.putInt(CompoundTagKeys.PRIMARY_COLOR, primaryColor.getId());
tag.putInt(CompoundTagKeys.SECONDARY_COLOR, secondaryColor.getId());
}

public static void setItemWhistleData(ItemStack stack, float whistlePitch) {
var tag = stack.getOrCreateTag();
tag.putFloat("whistlePitch", whistlePitch);
tag.putFloat(CompoundTagKeys.WHISTLE_PITCH, whistlePitch);
}

public static float getWhistlePitch(ItemStack stack) {
var tag = stack.getTag();
if (tag == null || !tag.contains("whistlePitch", Tag.TAG_FLOAT))
if (tag == null || !tag.contains(CompoundTagKeys.WHISTLE_PITCH, Tag.TAG_FLOAT))
return -1;
return tag.getFloat("whistlePitch");
return tag.getFloat(CompoundTagKeys.WHISTLE_PITCH);
}

public static void setOwnerData(ItemStack stack, GameProfile owner) {
Expand All @@ -91,17 +92,17 @@ public static GameProfile getOwner(ItemStack stack) {

public static DyeColor getPrimaryColor(ItemStack stack) {
var tag = stack.getOrCreateTag();
if (tag.contains("primaryColor", Tag.TAG_INT)) {
return DyeColor.byId(tag.getInt("primaryColor"));
if (tag.contains(CompoundTagKeys.PRIMARY_COLOR, Tag.TAG_INT)) {
return DyeColor.byId(tag.getInt(CompoundTagKeys.PRIMARY_COLOR));
} else {
return ((LocomotiveItem) stack.getItem()).defaultPrimary;
}
}

public static DyeColor getSecondaryColor(ItemStack stack) {
var tag = stack.getOrCreateTag();
if (tag.contains("secondaryColor", Tag.TAG_INT)) {
return DyeColor.byId(tag.getInt("secondaryColor"));
if (tag.contains(CompoundTagKeys.SECONDARY_COLOR, Tag.TAG_INT)) {
return DyeColor.byId(tag.getInt(CompoundTagKeys.SECONDARY_COLOR));
} else {
return ((LocomotiveItem) stack.getItem()).defaultSecondary;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/mods/railcraft/world/item/TicketItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;
import com.mojang.authlib.GameProfile;
import mods.railcraft.Translations;
import mods.railcraft.api.core.CompoundTagKeys;
import mods.railcraft.util.PlayerUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.NbtUtils;
Expand Down Expand Up @@ -85,7 +86,7 @@ public static boolean setTicketData(ItemStack ticket, String dest, @Nullable Gam
if (owner == null)
return false;
var tag = ticket.getOrCreateTag();
tag.putString("dest", dest);
tag.putString(CompoundTagKeys.DEST, dest);
NbtUtils.writeGameProfile(tag, owner);
return true;
}
Expand All @@ -96,7 +97,7 @@ public static String getDestination(ItemStack ticket) {
var tag = ticket.getTag();
if (tag == null)
return "";
return tag.getString("dest");
return tag.getString(CompoundTagKeys.DEST);
}

@Nullable
Expand Down

0 comments on commit c1840a7

Please sign in to comment.