Skip to content

Commit

Permalink
Merge pull request #240 from MuradAkh/inventory-rework
Browse files Browse the repository at this point in the history
Prep 1.20.1 for release
  • Loading branch information
EDToaster authored Oct 7, 2023
2 parents 36639ff + ecf8803 commit 8c47485
Show file tree
Hide file tree
Showing 57 changed files with 480 additions and 411 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ minecraft {
runs {
client {
workingDirectory project.file('run')
jvmArg "-XX:+AllowEnhancedClassRedefinition"

// Recommended logging data for a userdev environment
// The markers can be changed as needed.
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/dev/murad/shipping/ShippingConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static class Server {

public static final ForgeConfigSpec.ConfigValue<Double> TUG_BASE_SPEED;

public static final ForgeConfigSpec.ConfigValue<Integer> STEAM_TUG_FUEL_MULTIPLIER;
public static final ForgeConfigSpec.ConfigValue<Double> STEAM_TUG_FUEL_MULTIPLIER;

public static final ForgeConfigSpec.ConfigValue<Integer> TUG_PATHFINDING_MULTIPLIER;
public static final ForgeConfigSpec.ConfigValue<Integer> ENERGY_TUG_BASE_CAPACITY;
Expand All @@ -67,7 +67,7 @@ public static class Server {
public static final ForgeConfigSpec.ConfigValue<Double> TRAIN_MAX_SPEED;
public static final ForgeConfigSpec.ConfigValue<Double> LOCO_BASE_SPEED;

public static final ForgeConfigSpec.ConfigValue<Integer> STEAM_LOCO_FUEL_MULTIPLIER;
public static final ForgeConfigSpec.ConfigValue<Double> STEAM_LOCO_FUEL_MULTIPLIER;
public static final ForgeConfigSpec.ConfigValue<Integer> ENERGY_LOCO_BASE_CAPACITY;
public static final ForgeConfigSpec.ConfigValue<Integer> ENERGY_LOCO_BASE_ENERGY_USAGE;
public static final ForgeConfigSpec.ConfigValue<Integer> ENERGY_LOCO_BASE_MAX_CHARGE_RATE;
Expand Down Expand Up @@ -137,8 +137,8 @@ public static class Server {
.defineInRange("tugPathfindMult", 1, 1, 10);

STEAM_TUG_FUEL_MULTIPLIER =
BUILDER.comment("Increases the burn duration of Steam tug fuel by N times when compared to furnace, must be an integer >= 1. Default 4.")
.defineInRange("steamTugFuelMultiplier", 4, 1, Integer.MAX_VALUE);
BUILDER.comment("Increases the burn duration of Steam tug fuel by N times when compared to furnace, must be >= 0.01. Default 4.0.")
.defineInRange("steamTugFuelMultiplier", 4.0, 0.01, Double.MAX_VALUE);

ENERGY_TUG_BASE_CAPACITY =
BUILDER.comment("Base maximum capacity of the Energy tug in FE, must be an integer >= 1. Default 10000.")
Expand Down Expand Up @@ -185,8 +185,8 @@ public static class Server {
.defineInRange("locoBaseSpeed", 0.5, 0.01, 0.9);

STEAM_LOCO_FUEL_MULTIPLIER =
BUILDER.comment("Increases the burn duration of Steam locomotive fuel by N times when compared to furnace, must be an integer >= 1. Default 4.")
.defineInRange("steamLocoFuelMultiplier", 4, 1, Integer.MAX_VALUE);
BUILDER.comment("Increases the burn duration of Steam locomotive fuel by N times when compared to furnace, must be >= 0.01. Default 4.0.")
.defineInRange("steamLocoFuelMultiplier", 4.0, 0.01, Double.MAX_VALUE);

ENERGY_LOCO_BASE_CAPACITY =
BUILDER.comment("Base maximum capacity of the Energy locomotive in FE, must be an integer >= 1. Default 10000.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraftforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;

// Unused
public class FishingBargeContainer extends AbstractItemHandlerContainer {
private final FishingBargeEntity fishingBargeEntity;

Expand Down Expand Up @@ -36,6 +37,7 @@ protected int getSlotNum() {

@Override
public boolean stillValid(@NotNull Player player) {
return fishingBargeEntity.stillValid(player);
return true;
// return fishingBargeEntity.stillValid(player);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.murad.shipping.entity.custom;

import net.minecraftforge.items.ItemStackHandler;

import java.util.Optional;

/**
* A train inventory provider is a barge or a car that provides inventories
* to other barges or cars ahead of it.
*/
public interface TrainInventoryProvider {

// TODO: fluid provider

// TODO: energy provider

default Optional<ItemStackHandler> getTrainInventoryHandler() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void push(Entity pEntity) {
}
if (!pEntity.noPhysics && !this.noPhysics) {
// fix carts with passengers falling behind
if (!this.hasPassenger(pEntity) || this.getDominant().isPresent()) {
if (!this.hasPassenger(pEntity) || this.getLeader().isPresent()) {
double d0 = pEntity.getX() - this.getX();
double d1 = pEntity.getZ() - this.getZ();
double d2 = d0 * d0 + d1 * d1;
Expand Down Expand Up @@ -530,12 +530,12 @@ public Type getMinecartType() {
}

@Override
public Optional<AbstractTrainCarEntity> getDominated() {
public Optional<AbstractTrainCarEntity> getFollower() {
return linkingHandler.follower;
}

@Override
public Optional<AbstractTrainCarEntity> getDominant() {
public Optional<AbstractTrainCarEntity> getLeader() {
return linkingHandler.leader;
}

Expand Down Expand Up @@ -599,8 +599,8 @@ private Optional<Pair<AbstractTrainCarEntity, AbstractTrainCarEntity>> findClose

}

return curr.filter(pair -> (!(pair.getFirst() instanceof AbstractLocomotiveEntity) || pair.getFirst().getDominated().isEmpty())
&& (!(pair.getSecond() instanceof AbstractLocomotiveEntity) || pair.getSecond().getDominated().isEmpty()));
return curr.filter(pair -> (!(pair.getFirst() instanceof AbstractLocomotiveEntity) || pair.getFirst().getFollower().isEmpty())
&& (!(pair.getSecond() instanceof AbstractLocomotiveEntity) || pair.getSecond().getFollower().isEmpty()));


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ public InteractionResult interact(Player pPlayer, InteractionHand pHand) {
if(!pHand.equals(InteractionHand.MAIN_HAND)){
return InteractionResult.PASS;
}

if(!this.level().isClientSide){
NetworkHooks.openScreen((ServerPlayer) pPlayer, createContainerProvider(), getDataAccessor()::write);

}

return InteractionResult.CONSUME;
}

private ItemStackHandler createLocoRouteItemHandler() {
return new ItemStackHandler(1) {
return new ItemStackHandler() {
@Override
protected int getStackLimit(int slot, @Nonnull ItemStack stack) {
return 1;
Expand All @@ -153,16 +153,6 @@ protected void onContentsChanged(int slot) {
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return stack.getItem() instanceof LocoRouteItem;
}

@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (!isItemValid(slot, stack)) {
return stack;
}

return super.insertItem(slot, stack, simulate);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@
import javax.annotation.Nullable;

public class EnergyLocomotiveEntity extends AbstractLocomotiveEntity implements ItemHandlerVanillaContainerWrapper, WorldlyContainer {
private final ItemStackHandler itemHandler = createHandler();
private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> itemHandler);
private final ItemStackHandler energyItemHandler = createHandler();
private final LazyOptional<IItemHandler> energyItemHandlerOpt = LazyOptional.of(() -> energyItemHandler);
private static final int MAX_ENERGY = ShippingConfig.Server.ENERGY_LOCO_BASE_CAPACITY.get();
private static final int MAX_TRANSFER = ShippingConfig.Server.ENERGY_LOCO_BASE_MAX_CHARGE_RATE.get();
private static final int ENERGY_USAGE = ShippingConfig.Server.ENERGY_LOCO_BASE_ENERGY_USAGE.get();

private final ReadWriteEnergyStorage internalBattery = new ReadWriteEnergyStorage(MAX_ENERGY, MAX_TRANSFER, Integer.MAX_VALUE);
private final LazyOptional<IEnergyStorage> holder = LazyOptional.of(() -> internalBattery);
private final LazyOptional<IEnergyStorage> internalBatteryOpt = LazyOptional.of(() -> internalBattery);

public EnergyLocomotiveEntity(EntityType<?> type, Level p_38088_) {
super(type, p_38088_);
public EnergyLocomotiveEntity(EntityType<?> type, Level level) {
super(type, level);
internalBattery.setEnergy(0);
}

public EnergyLocomotiveEntity(Level level, Double aDouble, Double aDouble1, Double aDouble2) {
super(ModEntityTypes.ENERGY_LOCOMOTIVE.get(), level, aDouble, aDouble1, aDouble2);
public EnergyLocomotiveEntity(Level level, Double x, Double y, Double z) {
super(ModEntityTypes.ENERGY_LOCOMOTIVE.get(), level, x, y, z);
internalBattery.setEnergy(0);
}

private ItemStackHandler createHandler() {
return new ItemStackHandler(1) {
return new ItemStackHandler() {
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return stack.getCapability(ForgeCapabilities.ENERGY).isPresent();
Expand All @@ -74,9 +74,9 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if (cap == ForgeCapabilities.ITEM_HANDLER) {
return handler.cast();
return energyItemHandlerOpt.cast();
} else if (cap == ForgeCapabilities.ENERGY) {
return holder.cast();
return internalBatteryOpt.cast();
}

return super.getCapability(cap, side);
Expand All @@ -98,7 +98,6 @@ public Component getDisplayName() {
return Component.translatable("entity.littlelogistics.energy_locomotive");
}

@Nullable
@Override
public AbstractContainerMenu createMenu(int i, @NotNull Inventory playerInventory, @NotNull Player player) {
return new EnergyHeadVehicleContainer<EnergyLocomotiveEntity>(i, level(), getDataAccessor(), playerInventory, player);
Expand All @@ -124,7 +123,7 @@ public EnergyHeadVehicleDataAccessor getDataAccessor() {
public void tick() {
// grab energy from capacitor
if (!level().isClientSide) {
IEnergyStorage capability = InventoryUtils.getEnergyCapabilityInSlot(0, itemHandler);
IEnergyStorage capability = InventoryUtils.getEnergyCapabilityInSlot(0, energyItemHandler);
if (capability != null) {
// simulate first
int toExtract = capability.extractEnergy(MAX_TRANSFER, true);
Expand All @@ -149,7 +148,7 @@ public ItemStack getPickResult() {

@Override
public ItemStackHandler getRawHandler() {
return itemHandler;
return energyItemHandler;
}

@Override
Expand All @@ -169,14 +168,14 @@ public boolean canTakeItemThroughFace(int index, @NotNull ItemStack itemStack, @

@Override
public void readAdditionalSaveData(@NotNull CompoundTag compound) {
itemHandler.deserializeNBT(compound.getCompound("inv"));
energyItemHandler.deserializeNBT(compound.getCompound("inv"));
internalBattery.readAdditionalSaveData(compound.getCompound("energy_storage"));
super.readAdditionalSaveData(compound);
}

@Override
public void addAdditionalSaveData(@NotNull CompoundTag compound) {
compound.put("inv", itemHandler.serializeNBT());
compound.put("inv", energyItemHandler.serializeNBT());
CompoundTag energyNBT = new CompoundTag();
internalBattery.addAdditionalSaveData(energyNBT);
compound.put("energy_storage", energyNBT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.murad.shipping.setup.ModEntityTypes;
import dev.murad.shipping.setup.ModItems;
import dev.murad.shipping.setup.ModSounds;
import dev.murad.shipping.util.FuelItemStackHandler;
import dev.murad.shipping.util.ItemHandlerVanillaContainerWrapper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand All @@ -22,8 +23,7 @@
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.FurnaceBlockEntity;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.common.util.LazyOptional;
Expand All @@ -34,34 +34,19 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Random;

public class SteamLocomotiveEntity extends AbstractLocomotiveEntity implements ItemHandlerVanillaContainerWrapper, WorldlyContainer {
private final ItemStackHandler itemHandler = createHandler();
private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> itemHandler);
private static final int FURNACE_FUEL_MULTIPLIER= ShippingConfig.Server.STEAM_LOCO_FUEL_MULTIPLIER.get();
private final FuelItemStackHandler fuelItemHandler = new FuelItemStackHandler();
private final LazyOptional<IItemHandler> handler = LazyOptional.of(() -> fuelItemHandler);

protected int burnTime = 0;
protected int burnCapacity = 0;

private ItemStackHandler createHandler() {
return new ItemStackHandler(1) {
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return FurnaceBlockEntity.isFuel(stack);
}
// This has to remain as ConfigValue as the class isn't reloaded when changing worlds
private static final ForgeConfigSpec.ConfigValue<Double> FURNACE_FUEL_MULTIPLIER = ShippingConfig.Server.STEAM_LOCO_FUEL_MULTIPLIER;

@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (!isItemValid(slot, stack)) {
return stack;
}
// How many ticks left on this fuel
protected int burnTime = 0;

return super.insertItem(slot, stack, simulate);
}
};
}
// Max number of ticks for this fuel
protected int burnCapacity = 0;

public boolean isLit() {
return burnTime > 0;
Expand Down Expand Up @@ -96,17 +81,11 @@ protected boolean tickFuel() {
burnTime--;
return true;
} else {
ItemStack stack = itemHandler.getStackInSlot(0);
if (!stack.isEmpty()) {
burnCapacity = (ForgeHooks.getBurnTime(stack, null) * FURNACE_FUEL_MULTIPLIER) - 1;
burnTime = burnCapacity - 1;
stack.shrink(1);
return true;
} else {
burnCapacity = 0;
burnTime = 0;
return false;
}
int burnTime = fuelItemHandler.tryConsumeFuel();
int adjustedBurnTime = (int) Math.ceil(burnTime * FURNACE_FUEL_MULTIPLIER.get());
this.burnCapacity = adjustedBurnTime;
this.burnTime = adjustedBurnTime;
return adjustedBurnTime > 0;
}
}

Expand Down Expand Up @@ -149,12 +128,12 @@ public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable D
return super.getCapability(cap, side);
}

public SteamLocomotiveEntity(EntityType<?> type, Level p_38088_) {
super(type, p_38088_);
public SteamLocomotiveEntity(EntityType<?> type, Level level) {
super(type, level);
}

public SteamLocomotiveEntity(Level level, Double aDouble, Double aDouble1, Double aDouble2) {
super(ModEntityTypes.STEAM_LOCOMOTIVE.get(), level, aDouble, aDouble1, aDouble2);
public SteamLocomotiveEntity(Level level, Double x, Double y, Double z) {
super(ModEntityTypes.STEAM_LOCOMOTIVE.get(), level, x, y, z);
}


Expand Down Expand Up @@ -192,20 +171,20 @@ public boolean canPlaceItemThroughFace(int index, @NotNull ItemStack itemStack,

@Override
public ItemStackHandler getRawHandler() {
return itemHandler;
return fuelItemHandler;
}

@Override
public void readAdditionalSaveData(@NotNull CompoundTag compound) {
itemHandler.deserializeNBT(compound.getCompound("inv"));
fuelItemHandler.deserializeNBT(compound.getCompound("fuelItems"));
burnTime = compound.contains("burn") ? compound.getInt("burn") : 0;
burnCapacity = compound.contains("burn_capacity") ? compound.getInt("burn_capacity") : 0;
super.readAdditionalSaveData(compound);
}

@Override
public void addAdditionalSaveData(@NotNull CompoundTag compound) {
compound.put("inv", itemHandler.serializeNBT());
compound.put("fuelItems", fuelItemHandler.serializeNBT());
compound.putInt("burn", burnTime);
compound.putInt("burn_capacity", burnCapacity);
super.addAdditionalSaveData(compound);
Expand Down
Loading

0 comments on commit 8c47485

Please sign in to comment.