Skip to content

Commit

Permalink
Small minecart refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Mar 13, 2024
1 parent bcd3e4e commit 1b24167
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mods.railcraft.api.carts.Routable;
import mods.railcraft.season.Seasons;
import mods.railcraft.world.entity.vehicle.Directional;
import net.minecraft.ChatFormatting;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
Expand Down Expand Up @@ -78,16 +79,27 @@ public void render(T cart, float yaw, float partialTicks,
}
poseStack.translate(0, 0.375F, 0);

if (cart.hasCustomName() && !Seasons.GHOST_TRAIN.equals(cart.getCustomName().getString())
&& !Seasons.POLAR_EXPRESS.equals(cart.getCustomName().getString())) {
this.renderNameTag(cart, cart.getCustomName(), poseStack, bufferSource, packedLight);
boolean renderName = false;
if (cart.hasCustomName()) {
var customName = cart.getCustomName().getString();
if (!Seasons.GHOST_TRAIN.equals(customName) && !Seasons.POLAR_EXPRESS.equals(customName)) {
this.renderNameTag(cart, cart.getCustomName(), poseStack, bufferSource, packedLight);
renderName = true;
}
}

if (cart instanceof Routable routable) {
String dest = routable.getDestination();
if (!StringUtils.isBlank(dest))
this.renderNameTag(cart, Component.literal(dest), poseStack, bufferSource,
packedLight);
if (!StringUtils.isBlank(dest)) {
poseStack.pushPose();
if (renderName) {
poseStack.translate(0, 0.3F, 0);
}
var destination = Component.literal(dest)
.withStyle(ChatFormatting.GOLD, ChatFormatting.ITALIC);
this.renderNameTag(cart, destination, poseStack, bufferSource, packedLight);
poseStack.popPose();
}
}

poseStack.mulPose(Axis.YP.rotationDegrees(180.0F - yaw));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import snownee.jade.api.EntityAccessor;
import snownee.jade.api.IEntityComponentProvider;
import snownee.jade.api.ITooltip;
import snownee.jade.api.Identifiers;
import snownee.jade.api.TooltipPosition;
import snownee.jade.api.config.IPluginConfig;

class LocomotiveComponent implements IEntityComponentProvider {
Expand All @@ -23,9 +25,15 @@ public void appendTooltip(ITooltip tooltip, EntityAccessor accessor, IPluginConf
var reverse = locomotive.isReverse() ? Translations.LookingAt.YES : Translations.LookingAt.NO;
tooltip.add(Component.translatable(Translations.LookingAt.REVERSE)
.append(Component.translatable(reverse)));
tooltip.remove(Identifiers.UNIVERSAL_ITEM_STORAGE);
}
}

@Override
public int getDefaultPriority() {
return TooltipPosition.TAIL;
}

@Override
public ResourceLocation getUid() {
return RailcraftConstants.rl("locomotive");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class EnergyMinecart extends RailcraftMinecart {
private final LazyOptional<EnergyStorage> cartBattery =
LazyOptional.of(() -> new EnergyStorage(MAX_CHARGE));

protected EnergyMinecart(EntityType<?> type, Level level) {
public EnergyMinecart(EntityType<?> type, Level level) {
super(type, level);
}

protected EnergyMinecart(EntityType<?> type, double x, double y, double z, Level level) {
super(type, x, y, z, level);
public EnergyMinecart(ItemStack itemStack, double x, double y, double z, Level level) {
super(itemStack, null, x, y, z, level);
}

@Override
Expand All @@ -38,10 +38,9 @@ public int getContainerSize() {

@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
if (ForgeCapabilities.ENERGY == capability) {
return this.cartBattery.cast();
}
return super.getCapability(capability, facing);
return ForgeCapabilities.ENERGY == capability
? this.cartBattery.cast()
: super.getCapability(capability, facing);
}

@Override
Expand Down Expand Up @@ -70,7 +69,7 @@ public void addAdditionalSaveData(CompoundTag tag) {

@Override
public ItemStack getPickResult() {
ItemStack itemStack = super.getPickResult();
var itemStack = super.getPickResult();
this.cartBattery.ifPresent(cell -> {
itemStack.getOrCreateTag().putInt("batteryEnergy", cell.getEnergyStored());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected FilteredMinecart(EntityType<?> type, Level level) {

protected FilteredMinecart(ItemStack itemStack, EntityType<?> type, double x, double y, double z,
Level level) {
super(type, x, y, z, level);
super(itemStack, type, x, y, z, level);
this.setFilter(getFilterFromCartItem(itemStack));
}

Expand All @@ -35,31 +35,22 @@ protected void defineSynchedData() {
this.entityData.define(FILTER, ItemStack.EMPTY);
}

public static ItemStack getFilterFromCartItem(ItemStack cartStack) {
return cartStack.getItem() instanceof PrototypedItem
? ((PrototypedItem) cartStack.getItem()).getPrototype(cartStack)
private static ItemStack getFilterFromCartItem(ItemStack cartStack) {
return cartStack.getItem() instanceof PrototypedItem prototypedItem
? prototypedItem.getPrototype(cartStack)
: ItemStack.EMPTY;
}

public static ItemStack addFilterToCartItem(ItemStack cartStack, ItemStack filterStack) {
if (!filterStack.isEmpty() && cartStack.getItem() instanceof PrototypedItem) {
((PrototypedItem) cartStack.getItem()).setPrototype(cartStack, filterStack);
private static ItemStack addFilterToCartItem(ItemStack cartStack, ItemStack filterStack) {
if (!filterStack.isEmpty() && cartStack.getItem() instanceof PrototypedItem prototypedItem) {
prototypedItem.setPrototype(cartStack, filterStack);
}
return cartStack;
}

public ItemStack getFilteredCartItem(ItemStack filterStack) {
ItemStack cartStack = this.getPickResult();
return cartStack.isEmpty() ? ItemStack.EMPTY : addFilterToCartItem(cartStack, filterStack);
}

@Override
public ItemStack getPickResult() {
ItemStack stack = this.getFilteredCartItem(this.getFilterItem());
if (!stack.isEmpty() && this.hasCustomName()) {
stack.setHoverName(this.getDisplayName());
}
return stack;
return addFilterToCartItem(super.getPickResult(), this.getFilterItem());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

public abstract class MaintenanceMinecart extends RailcraftMinecart {

private static final EntityDataAccessor<Byte> BLINK =
SynchedEntityData.defineId(MaintenanceMinecart.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Integer> BLINK =
SynchedEntityData.defineId(MaintenanceMinecart.class, EntityDataSerializers.INT);
protected static final double DRAG_FACTOR = 0.9;
private static final int BLINK_DURATION = 3;
private static final EntityDataAccessor<Mode> MODE =
Expand All @@ -39,14 +39,15 @@ protected MaintenanceMinecart(EntityType<?> type, Level level) {
super(type, level);
}

protected MaintenanceMinecart(EntityType<?> type, double x, double y, double z, Level level) {
super(type, x, y, z, level);
protected MaintenanceMinecart(ItemStack itemStack, EntityType<?> type, double x, double y,
double z, Level level) {
super(itemStack, type, x, y, z, level);
}

@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(BLINK, (byte) 0);
this.entityData.define(BLINK, 0);
this.entityData.define(MODE, Mode.ON);
}

Expand All @@ -69,14 +70,14 @@ public int getContainerSize() {
}

protected void blink() {
this.entityData.set(BLINK, (byte) BLINK_DURATION);
this.entityData.set(BLINK, BLINK_DURATION);
}

protected void setBlink(byte blink) {
protected void setBlink(int blink) {
this.entityData.set(BLINK, blink);
}

protected byte getBlink() {
protected int getBlink() {
return this.entityData.get(BLINK);
}

Expand All @@ -92,7 +93,7 @@ public void tick() {
}

if (this.isBlinking()) {
this.setBlink((byte) (this.getBlink() - 1));
this.setBlink(this.getBlink() - 1);
}
}

Expand All @@ -110,7 +111,7 @@ protected void applyNaturalSlowdown() {
@Override
protected void addAdditionalSaveData(CompoundTag tag) {
super.addAdditionalSaveData(tag);
tag.putString("mode", this.mode().getSerializedName());
tag.putString(CompoundTagKeys.MODE, this.mode().getSerializedName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected MaintenancePatternMinecart(EntityType<?> type, Level level) {
super(type, level);
}

protected MaintenancePatternMinecart(EntityType<?> type, double x, double y, double z,
Level level) {
super(type, x, y, z, level);
protected MaintenancePatternMinecart(ItemStack itemStack, EntityType<?> type, double x, double y,
double z, Level level) {
super(itemStack, type, x, y, z, level);
}

public Container getPattern() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mods.railcraft.world.entity.vehicle;

import java.util.Optional;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.Nullable;
import mods.railcraft.api.carts.ItemTransferHandler;
import mods.railcraft.api.carts.RollingStock;
Expand All @@ -15,12 +16,15 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.entity.vehicle.AbstractMinecartContainer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.RailShape;
Expand All @@ -45,10 +49,26 @@ protected RailcraftMinecart(EntityType<?> type, Level level) {
super(type, level);
}

protected RailcraftMinecart(EntityType<?> type, double x, double y, double z, Level level) {
protected RailcraftMinecart(EntityType<TunnelBore> type, double x, double y, double z,
Level level) {
super(type, x, y, z, level);
}

protected RailcraftMinecart(ItemStack itemStack, EntityType<?> type, double x, double y,
double z, Level level) {
super(type, x, y, z, level);
this.loadCustomName(itemStack);
}

private void loadCustomName(ItemStack itemStack) {
if (itemStack.hasCustomHoverName()) {
this.setCustomName(itemStack.getHoverName());
}
}

protected void loadFromItemStack(ItemStack itemStack) {
}

public Optional<Direction> travelDirection() {
return Optional.ofNullable(this.travelDirection);
}
Expand All @@ -65,7 +85,6 @@ protected void defineSynchedData() {

@Override
public Season getSeason() {
// TODO: 1.20.4+ use Season.fromName(this.entityData.get(SEASON));
return this.entityData.get(SEASON);
}

Expand All @@ -88,7 +107,7 @@ protected void readAdditionalSaveData(CompoundTag tag) {

@Override
public InteractionResult interact(Player player, InteractionHand hand) {
if (!player.level().isClientSide()) {
if (!this.level().isClientSide()) {
if (this.hasMenu()) {
NetworkHooks.openScreen((ServerPlayer) player, this,
data -> data.writeVarInt(this.getId()));
Expand All @@ -112,6 +131,33 @@ public void remove(RemovalReason reason) {
super.remove(reason);
}

@Override
public final void destroy(DamageSource source) {
this.kill();
if (this.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
var itemstack = this.getPickResult().copy();
if (this.hasCustomName()) {
itemstack.setHoverName(this.getCustomName());
}
this.spawnAtLocation(itemstack);
}
this.chestVehicleDestroyed(source, this.level(), this);
}

@Override
public ItemStack getPickResult() {
var itemStack = this.getDropItem().getDefaultInstance();
if (this.hasCustomName()) {
itemStack.setHoverName(this.getCustomName());
}
return itemStack;
}

@Override
protected Item getDropItem() {
throw new NotImplementedException();
}

@Override
public AbstractMinecart.Type getMinecartType() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public TankMinecart(EntityType<?> type, Level level) {

public TankMinecart(ItemStack itemStack, double x, double y, double z, Level level) {
super(itemStack, RailcraftEntityTypes.TANK_MINECART.get(), x, y, z, level);
this.loadFromItemStack(itemStack);
}

@Override
Expand Down Expand Up @@ -218,7 +219,24 @@ public boolean canProvidePulledFluid(RollingStock requester, FluidStack fluid) {

@Override
public ItemStack getPickResult() {
return RailcraftItems.TANK_MINECART.get().getDefaultInstance();
var itemStack = super.getPickResult();
if (!this.tank.isEmpty()) {
var tag = itemStack.getOrCreateTag();
var tankTag = new CompoundTag();
this.tank.writeToNBT(tankTag);
tag.put(CompoundTagKeys.TANK, tankTag);
}
return itemStack;
}

@Override
protected void loadFromItemStack(ItemStack itemStack) {
super.loadFromItemStack(itemStack);
var tag = itemStack.getTag();
if (tag == null) {
return;
}
this.tank.readFromNBT(tag.getCompound(CompoundTagKeys.TANK));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public TrackLayer(EntityType<?> type, Level level) {
}

public TrackLayer(ItemStack itemStack, double x, double y, double z, ServerLevel level) {
super(RailcraftEntityTypes.TRACK_LAYER.get(), x, y, z, level);
super(itemStack, RailcraftEntityTypes.TRACK_LAYER.get(), x, y, z, level);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public TrackRelayer(EntityType<?> type, Level level) {
}

public TrackRelayer(ItemStack itemStack, double x, double y, double z, ServerLevel level) {
super(RailcraftEntityTypes.TRACK_RELAYER.get(), x, y, z, level);
super(itemStack, RailcraftEntityTypes.TRACK_RELAYER.get(), x, y, z, level);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TrackRemover(EntityType<?> type, Level level) {
}

public TrackRemover(ItemStack itemStack, double x, double y, double z, ServerLevel level) {
super(RailcraftEntityTypes.TRACK_REMOVER.get(), x, y, z, level);
super(itemStack, RailcraftEntityTypes.TRACK_REMOVER.get(), x, y, z, level);
}

@Override
Expand Down

0 comments on commit 1b24167

Please sign in to comment.