Skip to content

Commit

Permalink
forge item salepoint implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
techno-sam committed Aug 4, 2024
1 parent 934be99 commit 4480adc
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected void renderBg(@NotNull GuiGraphics graphics, float partialTick, int mo
Couple<Integer> referenceAndSpurs = NumismaticsConfig.common().referenceCoin.get().convert(menu.contentHolder.getTotalPrice());
int reference = referenceAndSpurs.getFirst();
int spurs = referenceAndSpurs.getSecond();
Component balanceLabel = Components.translatable("block.numismatics.brass_depositor.tooltip.price",
Component balanceLabel = Components.translatable("gui.numismatics.salepoint.price",
TextUtils.formatInt(reference), NumismaticsConfig.common().referenceCoin.get().getName(reference), spurs);
graphics.drawCenteredString(font, balanceLabel, x + (background.width - 8) / 2, y + 21, 0xFFFFFF);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

public class InvalidatableWrappingItemBuffer extends InvalidatableAbstractBuffer<ItemStack> {

Expand All @@ -37,9 +38,23 @@ protected void afterInvalidate() {

@Override
protected int copyToBufferInternal(ItemStack source, boolean simulate) {
final SimpleContainer buffer$ = this.buffer;
SimpleContainer buffer = this.buffer;
if (simulate) { // lazy but it works
buffer = new SimpleContainer(buffer.getContainerSize());
buffer = new SimpleContainer(buffer.getContainerSize()) {
@Override
public @NotNull ItemStack addItem(@NotNull ItemStack stack) {
if (!buffer$.canPlaceItem(0, stack))
return stack;

return super.addItem(stack);
}

@Override
public boolean canPlaceItem(int index, @NotNull ItemStack stack) {
return buffer$.canPlaceItem(index, stack);
}
};
for (int i = 0; i < buffer.getContainerSize(); i++) {
buffer.setItem(i, this.buffer.getItem(i).copy());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public ItemStack addItem(ItemStack stack) {

return super.addItem(stack);
}

@Override
public boolean canPlaceItem(int index, ItemStack stack) {
return super.canPlaceItem(index, stack) && VendorBlockEntity.matchesFilterItem(filter, stack);
}
};
private @NotNull InvalidatableWrappingItemBuffer bufferWrapper = createBufferWrapper(buffer);
private @Nullable Runnable changedCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public boolean canTransfer() {
}

@Override
public void addBehaviours(List<BlockEntityBehaviour> behaviours) { // no transfer because no entity so sad.
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
super.addBehaviours(behaviours);
railway$salepointBehaviour = new ItemSalepointTargetBehaviour(this) {
private boolean underControl = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.content.salepoint.containers.forge;

import dev.ithundxr.createnumismatics.content.salepoint.containers.InvalidatableWrappingItemBuffer;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import org.jetbrains.annotations.NotNull;

public class InvalidatableWrappingItemBufferHandler extends InvalidatableWrappingItemBuffer implements IItemHandlerModifiable {
public InvalidatableWrappingItemBufferHandler(SimpleContainer buffer) {
super(buffer);
}

@Override
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
if (!isValid() || slot >= getSlots())
throw new RuntimeException("Invalid slot");

buffer.setItem(slot, stack);
}

@Override
public int getSlots() {
if (!isValid())
return 0;

return buffer.getContainerSize();
}

@Override
public @NotNull ItemStack getStackInSlot(int slot) {
if (!isValid() || slot >= getSlots())
return ItemStack.EMPTY;

return buffer.getItem(slot);
}

@Override
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (!isValid() || slot >= getSlots())
return stack;

int inserted = copyToBufferInternal(stack, simulate);
if (inserted == 0)
return stack;

return stack.copyWithCount(stack.getCount() - inserted);
}

@Override
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
if (!isValid() || slot >= getSlots())
return ItemStack.EMPTY;

if (simulate) {
ItemStack stack = buffer.getItem(slot);
return stack.copyWithCount(Math.min(amount, stack.getCount()));
} else {
return buffer.removeItem(slot, amount);
}
}

@Override
public int getSlotLimit(int slot) {
if (!isValid() || slot >= getSlots())
return 0;

return buffer.getItem(slot).getMaxStackSize();
}

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
if (!isValid() || slot >= getSlots())
return false;

return buffer.canPlaceItem(slot, stack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.content.salepoint.states.forge;

import dev.ithundxr.createnumismatics.content.salepoint.containers.InvalidatableWrappingItemBuffer;
import dev.ithundxr.createnumismatics.content.salepoint.containers.forge.InvalidatableWrappingItemBufferHandler;
import net.minecraft.world.SimpleContainer;

public class ItemSalepointStateImpl {
public static InvalidatableWrappingItemBuffer createBufferWrapper(SimpleContainer buffer) {
return new InvalidatableWrappingItemBufferHandler(buffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.forge.mixin;

import com.simibubi.create.foundation.item.ItemHandlerWrapper;
import net.minecraftforge.items.IItemHandlerModifiable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ItemHandlerWrapper.class)
public interface ItemHandlerWrapperAccessor {
@Accessor("wrapped")
void setWrapped(IItemHandlerModifiable wrapped);

@Accessor("wrapped")
IItemHandlerModifiable getWrapped();
}
Loading

0 comments on commit 4480adc

Please sign in to comment.