Skip to content

Commit

Permalink
Small refactors and fixes #87
Browse files Browse the repository at this point in the history
  • Loading branch information
Sm0keySa1m0n committed Sep 1, 2023
1 parent a04d1c7 commit 5f7f475
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/api/java/mods/railcraft/api/carts/RollingStock.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface RollingStock {

static RollingStock getOrThrow(AbstractMinecart minecart) {
return minecart.getCapability(CAPABILITY)
.orElseThrow(() -> new IllegalStateException("MinecartExtension missing on " + minecart));
.orElseThrow(() -> new IllegalStateException("RollingStock missing on " + minecart));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public float getOptimalDistance(RollingStock cart) {
}

@Override
protected Container getTicketInventory() {
protected Container ticketContainer() {
return invTicket;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public float getOptimalDistance(RollingStock cart) {
}

@Override
protected Container getTicketInventory() {
protected Container ticketContainer() {
return this.ticketInventory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void setEmblem(@Nullable String emblem) {
* Gets the destination ticket item.
*/
public ItemStack getDestItem() {
return getTicketInventory().getItem(1);
return ticketContainer().getItem(1);
}

@Override
Expand Down Expand Up @@ -345,7 +345,7 @@ public boolean setDestination(ItemStack ticket) {
var destination = TicketItem.getDestination(ticket);
if (!destination.equals(this.getDestination())) {
this.setDestination(destination);
this.getTicketInventory().setItem(1, TicketItem.copyTicket(ticket));
this.ticketContainer().setItem(1, TicketItem.copyTicket(ticket));
return true;
}
}
Expand Down Expand Up @@ -536,10 +536,10 @@ private void snowEffect(double x, double y, double z) {
this.level().addParticle(ParticleTypes.ITEM_SNOWBALL, x, y, z, vx, vy, vz);
}

protected abstract Container getTicketInventory();
protected abstract Container ticketContainer();

private void processTicket() {
Container invTicket = this.getTicketInventory();
Container invTicket = this.ticketContainer();
ItemStack stack = invTicket.getItem(0);
if (stack.getItem() instanceof TicketItem) {
if (setDestination(stack)) {
Expand Down Expand Up @@ -708,7 +708,7 @@ private boolean isVelocityHigherThan(float velocity) {

@Override
public void remove(RemovalReason reason) {
this.getTicketInventory().setItem(1, ItemStack.EMPTY);
this.ticketContainer().setItem(1, ItemStack.EMPTY);
super.remove(reason);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public class SteamLocomotive extends BaseSteamLocomotive implements WorldlyConta
private static final int TICKET_SLOT = 7;
private static final int[] SLOTS = ContainerTools.buildSlotArray(0, 7);

private final ContainerMapper fuelInventory = ContainerMapper.make(this, FUEL_SLOT, 1);
private final ContainerMapper extraFuelInventory =
private final ContainerMapper fuelContainer = ContainerMapper.make(this, FUEL_SLOT, 1);
private final ContainerMapper extraFuelContainer =
ContainerMapper.make(this, EXTRA_FUEL_SLOT_A, 3);
private final ContainerMapper fuelContainer = ContainerMapper.make(this, FUEL_SLOT, 4);
private final ContainerMapper ticketInventory =
private final ContainerMapper allFuelContainer = ContainerMapper.make(this, FUEL_SLOT, 4);
private final ContainerMapper ticketContainer =
new ContainerMapper(this, TICKET_SLOT, 2).ignoreItemChecks();

public SteamLocomotive(EntityType<?> type, Level level) {
Expand Down Expand Up @@ -80,24 +80,24 @@ protected DyeColor getDefaultSecondaryColor() {
public void tick() {
super.tick();

if (this.level().isClientSide()) {
if (this.level().isClientSide() || this.isRemoved()) {
return;
}
extraFuelInventory.moveOneItemTo(fuelInventory);
this.extraFuelContainer.moveOneItemTo(this.fuelContainer);
// fuelInventory.moveOneItemTo(invWaterOutput,
// (ItemStack item) -> (ForgeHooks.getBurnTime(item) > 0));

var extension = RollingStock.getOrThrow(this);
ItemStack stack =
CartUtil.transferService().pullStack(extension, this.extraFuelInventory::canFit);
if (!stack.isEmpty()) {
extraFuelInventory.insert(stack);
var pulledFuel =
CartUtil.transferService().pullStack(extension, this.extraFuelContainer::canFit);
if (!pulledFuel.isEmpty()) {
this.extraFuelContainer.insert(pulledFuel);
}
if (isSafeToFill() && waterTank.getFluidAmount() < waterTank.getCapacity() / 2) {
var pulled =
if (this.isSafeToFill() && this.waterTank.getFluidAmount() < this.waterTank.getCapacity() / 2) {
var pulledWater =
CartUtil.transferService().pullFluid(extension, new FluidStack(Fluids.WATER, 1));
if (pulled != null) {
waterTank.fill(pulled, FluidAction.EXECUTE);
if (pulledWater != null) {
this.waterTank.fill(pulledWater, FluidAction.EXECUTE);
}
}
}
Expand All @@ -108,17 +108,17 @@ public boolean needsFuel() {
if (water.isEmpty() || water.getAmount() < this.waterTank.getCapacity() / 3) {
return true;
}
int numItems = this.fuelContainer.countItems(item -> ForgeHooks.getBurnTime(item, null) > 0);
int numItems = this.allFuelContainer.countItems(item -> ForgeHooks.getBurnTime(item, null) > 0);
if (numItems == 0) {
return true;
}
int maxItems = this.fuelContainer.countMaxItemStackSize();
int maxItems = this.allFuelContainer.countMaxItemStackSize();
return (float) numItems / (float) maxItems < 0.25F;
}

@Override
protected Container getTicketInventory() {
return ticketInventory;
protected Container ticketContainer() {
return this.ticketContainer;
}

@Override
Expand All @@ -144,13 +144,15 @@ public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction side)
@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
return switch (slot) {
case FUEL_SLOT, EXTRA_FUEL_SLOT_A, EXTRA_FUEL_SLOT_B, EXTRA_FUEL_SLOT_C ->
ForgeHooks.getBurnTime(stack, null) > 0;
case FUEL_SLOT,
EXTRA_FUEL_SLOT_A,
EXTRA_FUEL_SLOT_B,
EXTRA_FUEL_SLOT_C -> ForgeHooks.getBurnTime(stack, null) > 0;
case SLOT_WATER_INPUT ->
// if (FluidItemHelper.getFluidStackInContainer(stack)
// .filter(fluidStack -> fluidStack.getAmount() > FluidTools.BUCKET_VOLUME).isPresent()) {
// return false;
// } we allow tanks instafilling.
// if (FluidItemHelper.getFluidStackInContainer(stack)
// .filter(fluidStack -> fluidStack.getAmount() > FluidTools.BUCKET_VOLUME).isPresent()) {
// return false;
// } we allow tanks instafilling.
FluidItemHelper.containsFluid(stack, Fluids.WATER);
case TICKET_SLOT -> TicketItem.FILTER.test(stack);
default -> false;
Expand Down

0 comments on commit 5f7f475

Please sign in to comment.