Skip to content

Commit

Permalink
Added logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Aug 26, 2023
1 parent edcce8f commit ab497cb
Show file tree
Hide file tree
Showing 15 changed files with 397 additions and 81 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ minecraft {
property 'forge.logging.console.level', 'debug'
property 'forge.enabledGameTestNamespaces', 'railcraft'

// Enables better hot reloading if using the JetBrains Runtime
jvmArgs '-XX:+AllowEnhancedClassRedefinition', '-XX:+IgnoreUnrecognizedVMOptions', '-XX:+AllowRedefinitionToAddDeleteMethods'

args '--mixin', 'railcraft.mixins.json'

mods {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mods/railcraft/client/ClientManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import mods.railcraft.client.gui.screen.inventory.CokeOvenScreen;
import mods.railcraft.client.gui.screen.inventory.CreativeLocomotiveScreen;
import mods.railcraft.client.gui.screen.inventory.CrusherScreen;
import mods.railcraft.client.gui.screen.inventory.DumpingTrackScreen;
import mods.railcraft.client.gui.screen.inventory.ElectricLocomotiveScreen;
import mods.railcraft.client.gui.screen.inventory.FeedStationScreen;
import mods.railcraft.client.gui.screen.inventory.FluidFueledSteamBoilerScreen;
Expand Down Expand Up @@ -135,6 +136,7 @@ private static void handleClientSetup(FMLClientSetupEvent event) {
SwitchTrackRouterScreen::new);
MenuScreens.register(RailcraftMenuTypes.TUNNEL_BORE.get(), TunnelBoreScreen::new);
MenuScreens.register(RailcraftMenuTypes.ROUTING_TRACK.get(), RoutingTrackScreen::new);
MenuScreens.register(RailcraftMenuTypes.DUMPING_TRACK.get(), DumpingTrackScreen::new);
}

private static void handleItemColors(RegisterColorHandlersEvent.Item event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mods.railcraft.client.gui.screen.inventory;

import mods.railcraft.Railcraft;
import mods.railcraft.Translations;
import mods.railcraft.client.util.GuiUtil;
import mods.railcraft.world.inventory.DumpingTrackMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class DumpingTrackScreen extends RailcraftMenuScreen<DumpingTrackMenu> {

private static final ResourceLocation WIDGETS_TEXTURE_LOCATION =
new ResourceLocation(Railcraft.ID, "textures/gui/container/dumping_track.png");

private static final Component FILTERS =
Component.translatable(Translations.Screen.ITEM_MANIPULATOR_FILTERS);
private static final Component CARTS =
Component.translatable(Translations.Screen.CART_FILTERS);

public DumpingTrackScreen(DumpingTrackMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}

@Override
public ResourceLocation getWidgetsTexture() {
return WIDGETS_TEXTURE_LOCATION;
}

@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
super.renderLabels(guiGraphics, mouseX, mouseY);
GuiUtil.drawCenteredString(guiGraphics, this.font, FILTERS, 250, 26);
GuiUtil.drawCenteredString(guiGraphics, this.font, CARTS, 100, 35);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public static boolean isItem(ItemStack stack, @Nullable Item item) {
public static boolean matchesFilter(ItemStack filter, ItemStack stack) {
if (stack.isEmpty() || filter.isEmpty())
return false;
if (filter.getItem() instanceof Filter) {
return ((Filter) filter.getItem()).matches(filter, stack);
if (filter.getItem() instanceof Filter filterItem) {
return filterItem.matches(filter, stack);
}
return isItemEqual(stack, filter);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mods/railcraft/util/container/StackFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ public static Predicate<ItemStack> isCart(@Nullable final AbstractMinecart cart)
return false;
}

ItemStack cartItem = cart.getPickResult();
boolean matches = !itemStack.isEmpty() && ItemStack.isSameItem(cartItem, itemStack);
var cartItem = cart.getPickResult();
boolean matches = ItemStack.isSameItem(cartItem, itemStack);

if (itemStack.hasCustomHoverName()) {
return matches && itemStack.getDisplayName().getContents()
.equals(cart.getPickResult().getDisplayName().getContents());
.equals(cartItem.getDisplayName().getContents());
}

return matches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static ContainerManipulator<ModifiableSlotAccessor> of(Container container) {
return slots::stream;
}

static ContainerManipulator<ModifiableSlotAccessor> of(
WorldlyContainer container, Direction face) {
static ContainerManipulator<ModifiableSlotAccessor> of(WorldlyContainer container,
Direction face) {
var slots = ContainerSlotAccessor.createSlots(container, face).toList();
return slots::stream;
}
Expand Down Expand Up @@ -95,6 +95,17 @@ default Stream<ItemStack> streamItems() {
return this.stream().filter(SlotAccessor::hasItem).map(SlotAccessor::item);
}

/**
* Attempt to add the stack to the inventory returning the remainder.
* <p>
* If the entire stack was accepted, it returns an empty stack.
*
* @return The remainder
*/
default ItemStack insert(ItemStack stack) {
return this.insert(stack, false);
}

/**
* Attempt to add the stack to the inventory returning the remainder.
* <p>
Expand Down Expand Up @@ -124,15 +135,22 @@ default ItemStack insert(ItemStack stack, boolean simulate) {
}

/**
* Removes up to maxAmount items in one slot matching the filter.
* Removes and returns a single item from the inventory.
*
* @return An ItemStack
*/
default ItemStack extract(int maxAmount, Predicate<ItemStack> filter, boolean simulate) {
return this.stream()
.filter(slot -> slot.matches(filter))
.map(slot -> slot.extract(maxAmount, simulate))
.filter(item -> !item.isEmpty())
.findFirst()
.orElse(ItemStack.EMPTY);
default ItemStack extract() {
return this.extract(StackFilter.ALL);
}

/**
* Removes and returns a single item from the inventory that matches the filter.
*
* @param filter the filter to match against
* @return An ItemStack
*/
default ItemStack extract(ItemStack... filter) {
return this.extract(StackFilter.anyOf(filter));
}

/**
Expand All @@ -143,22 +161,25 @@ default ItemStack extract(Predicate<ItemStack> filter) {
}

/**
* Removes and returns a single item from the inventory.
*
* @return An ItemStack
* Removes up to maxAmount items in one slot matching the filter.
*/
default ItemStack extract() {
return this.extract(StackFilter.ALL);
default ItemStack extract(int maxAmount, Predicate<ItemStack> filter, boolean simulate) {
return this.stream()
.filter(slot -> slot.matches(filter))
.map(slot -> slot.extract(maxAmount, simulate))
.filter(item -> !item.isEmpty())
.findFirst()
.orElse(ItemStack.EMPTY);
}

/**
* Removes and returns a single item from the inventory that matches the filter.
* Attempts to move a single item from one inventory to another.
*
* @param filter the filter to match against
* @return An ItemStack
* @param dest the destination inventory
* @return null if nothing was moved, the stack moved otherwise
*/
default ItemStack extract(ItemStack... filter) {
return this.extract(StackFilter.anyOf(filter));
default ItemStack moveOneItemTo(ContainerManipulator<?> dest) {
return this.moveOneItemTo(dest, itemStack -> true);
}

default ItemStack moveOneItemTo(ContainerManipulator<?> dest, Predicate<ItemStack> filter) {
Expand All @@ -175,17 +196,26 @@ default ItemStack moveOneItemTo(ContainerManipulator<?> dest, Predicate<ItemStac
}

/**
* Checks if inventory will accept the ItemStack.
* Attempts to move a single itemstack from one inventory to another.
*
* @param stack The ItemStack
* @return true if room for stack
* @param dest the destination inventory
* @return null if nothing was moved, the stack moved otherwise
*/
default boolean willAccept(ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
var newStack = ContainerTools.copyOne(stack);
return this.stream().anyMatch(slot -> slot.isValid(newStack));
default ItemStack moveOneItemStackTo(ContainerManipulator<?> dest) {
return this.moveOneItemStackTo(dest, itemStack -> true);
}

default ItemStack moveOneItemStackTo(ContainerManipulator<?> dest, Predicate<ItemStack> filter) {
return this.stream()
.filter(slot -> slot.matches(filter))
.filter(slot -> !slot.simulateExtract(64).isEmpty())
.filter(slot -> dest.insert(slot.simulateExtract(64), true).isEmpty())
.map(slot -> {
dest.insert(slot.simulateExtract(64), false);
return slot.extract(64, false);
})
.findFirst()
.orElse(ItemStack.EMPTY);
}

/**
Expand All @@ -199,24 +229,27 @@ default boolean willAcceptAny(List<ItemStack> stacks) {
}

/**
* Checks if there is room for the ItemStack in the inventory.
* Checks if inventory will accept the ItemStack.
*
* @param stack The ItemStack
* @return true if room for stack
*/
default boolean canFit(ItemStack stack) {
return this.insert(stack, true).isEmpty();
default boolean willAccept(ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
var newStack = ContainerTools.copyOne(stack);
return this.stream().anyMatch(slot -> slot.isValid(newStack));
}

/**
* Attempt to add the stack to the inventory returning the remainder.
*
* If the entire stack was accepted, it returns an empty stack.
* Checks if there is room for the ItemStack in the inventory.
*
* @return The remainder
* @param stack The ItemStack
* @return true if room for stack
*/
default ItemStack insert(ItemStack stack) {
return this.insert(stack, false);
default boolean canFit(ItemStack stack) {
return this.insert(stack, true).isEmpty();
}

default Optional<T> findFirstExtractable(Predicate<ItemStack> filter) {
Expand All @@ -225,16 +258,6 @@ default Optional<T> findFirstExtractable(Predicate<ItemStack> filter) {
.findFirst();
}

/**
* Attempts to move a single item from one inventory to another.
*
* @param dest the destination inventory
* @return null if nothing was moved, the stack moved otherwise
*/
default ItemStack moveOneItemTo(ContainerManipulator<?> dest) {
return this.moveOneItemTo(dest, itemStack -> true);
}

/**
* Returns true if the inventory contains the specified item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ default ItemStack extract() {
}

default ItemStack simulateExtract() {
return this.extract(1, true);
return this.simulateExtract(1);
}

default ItemStack simulateExtract(int amount) {
return this.extract(amount, true);
}

ItemStack extract(int amount, boolean simulate);
Expand Down
38 changes: 17 additions & 21 deletions src/main/java/mods/railcraft/world/entity/vehicle/TrackRelayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mods.railcraft.world.entity.RailcraftEntityTypes;
import mods.railcraft.world.inventory.TrackRelayerMenu;
import mods.railcraft.world.item.RailcraftItems;
import mods.railcraft.world.level.block.entity.track.DumpingTrackBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -59,48 +60,43 @@ public void tick() {
}

private void replace() {
BlockPos pos = BlockPos.containing(position());
var pos = BlockPos.containing(position());

if (TrackUtil.isRailBlockAt(level(), pos.below()))
if (TrackUtil.isRailBlockAt(level(), pos.below())) {
pos = pos.below();
}

var blockstate = level().getBlockState(pos);
var block = blockstate.getBlock();

if (TrackUtil.isRail(block)) {
ItemStack trackExist = patternContainer.getItem(SLOT_EXISTS);
ItemStack trackStock = patternContainer.getItem(SLOT_STOCK);
var trackExist = patternContainer.getItem(SLOT_EXISTS);
var trackStock = patternContainer.getItem(SLOT_STOCK);

boolean nextToSuspended = false;
//TODO: Implement when we have Dumping tracks
/*for (var direction : HORIZONTAL_DIRECTION) {
for (var direction : HORIZONTAL_DIRECTION) {
var blockEntity = level().getBlockEntity(pos.offset(direction.getNormal()));
if (blockEntity instanceof IOutfittedTrackTile) {
IOutfittedTrackTile track = (IOutfittedTrackTile) tile;
if (track.getTrackKitInstance() instanceof TrackKitSuspended) {
nextToSuspended = true;
break;
}
if (blockEntity instanceof DumpingTrackBlockEntity) {
nextToSuspended = true;
break;
}
}*/
}

if (nextToSuspended)
if (nextToSuspended) {
return;
}

if (!trackExist.isEmpty() && !trackStock.isEmpty())
if (!trackExist.isEmpty() && !trackStock.isEmpty()) {
if (trackExist.getItem() instanceof BlockItem trackExistsBlockItem) {
//ITrackItem trackItem = (ITrackItem) trackExist.getItem();
if (trackExistsBlockItem.getBlock() == block) {
var blockEntity = level().getBlockEntity(pos);
//if (trackExistsBlockItem.isPlacedTileEntity(trackExist, blockEntity)) {
var trackShape = removeOldTrack(pos, blockstate);
placeNewTrack(pos, SLOT_STOCK, trackShape);
//}
var trackShape = removeOldTrack(pos, blockstate);
placeNewTrack(pos, SLOT_STOCK, trackShape);
}
} else if (ContainerTools.isStackEqualToBlock(trackExist, block)) {
var trackShape = removeOldTrack(pos, blockstate);
placeNewTrack(pos, SLOT_STOCK, trackShape);
}
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/mods/railcraft/world/inventory/DumpingTrackMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mods.railcraft.world.inventory;

import mods.railcraft.world.inventory.slot.BlockFilterSlot;
import mods.railcraft.world.inventory.slot.PhantomMinecartSlot;
import mods.railcraft.world.level.block.entity.track.DumpingTrackBlockEntity;
import net.minecraft.world.entity.player.Inventory;

public class DumpingTrackMenu extends RailcraftMenu {

public DumpingTrackMenu(int id, Inventory inventory, DumpingTrackBlockEntity blockEntity) {
super(RailcraftMenuTypes.DUMPING_TRACK.get(), id, inventory.player, blockEntity::isStillValid);

for (int i = 0; i < 3; i++) {
this.addSlot(new PhantomMinecartSlot(blockEntity.getCartFilter(), i, 25 + i * 18, 45));
}

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
this.addSlot(new BlockFilterSlot(blockEntity.getItemFilter(), i * 3 + j,
98 + j * 18, 36 + i * 18));
}
}

this.addInventorySlots(inventory);
}
}

0 comments on commit ab497cb

Please sign in to comment.