Skip to content

Commit 4480adc

Browse files
committed
forge item salepoint implementation
1 parent 934be99 commit 4480adc

File tree

9 files changed

+375
-3
lines changed

9 files changed

+375
-3
lines changed

common/src/main/java/dev/ithundxr/createnumismatics/content/salepoint/SalepointConfigScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected void renderBg(@NotNull GuiGraphics graphics, float partialTick, int mo
148148
Couple<Integer> referenceAndSpurs = NumismaticsConfig.common().referenceCoin.get().convert(menu.contentHolder.getTotalPrice());
149149
int reference = referenceAndSpurs.getFirst();
150150
int spurs = referenceAndSpurs.getSecond();
151-
Component balanceLabel = Components.translatable("block.numismatics.brass_depositor.tooltip.price",
151+
Component balanceLabel = Components.translatable("gui.numismatics.salepoint.price",
152152
TextUtils.formatInt(reference), NumismaticsConfig.common().referenceCoin.get().getName(reference), spurs);
153153
graphics.drawCenteredString(font, balanceLabel, x + (background.width - 8) / 2, y + 21, 0xFFFFFF);
154154

common/src/main/java/dev/ithundxr/createnumismatics/content/salepoint/containers/InvalidatableWrappingItemBuffer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import net.minecraft.world.SimpleContainer;
2222
import net.minecraft.world.item.ItemStack;
23+
import org.jetbrains.annotations.NotNull;
2324

2425
public class InvalidatableWrappingItemBuffer extends InvalidatableAbstractBuffer<ItemStack> {
2526

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

3839
@Override
3940
protected int copyToBufferInternal(ItemStack source, boolean simulate) {
41+
final SimpleContainer buffer$ = this.buffer;
4042
SimpleContainer buffer = this.buffer;
4143
if (simulate) { // lazy but it works
42-
buffer = new SimpleContainer(buffer.getContainerSize());
44+
buffer = new SimpleContainer(buffer.getContainerSize()) {
45+
@Override
46+
public @NotNull ItemStack addItem(@NotNull ItemStack stack) {
47+
if (!buffer$.canPlaceItem(0, stack))
48+
return stack;
49+
50+
return super.addItem(stack);
51+
}
52+
53+
@Override
54+
public boolean canPlaceItem(int index, @NotNull ItemStack stack) {
55+
return buffer$.canPlaceItem(index, stack);
56+
}
57+
};
4358
for (int i = 0; i < buffer.getContainerSize(); i++) {
4459
buffer.setItem(i, this.buffer.getItem(i).copy());
4560
}

common/src/main/java/dev/ithundxr/createnumismatics/content/salepoint/states/ItemSalepointState.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public ItemStack addItem(ItemStack stack) {
6060

6161
return super.addItem(stack);
6262
}
63+
64+
@Override
65+
public boolean canPlaceItem(int index, ItemStack stack) {
66+
return super.canPlaceItem(index, stack) && VendorBlockEntity.matchesFilterItem(filter, stack);
67+
}
6368
};
6469
private @NotNull InvalidatableWrappingItemBuffer bufferWrapper = createBufferWrapper(buffer);
6570
private @Nullable Runnable changedCallback;

fabric/src/main/java/dev/ithundxr/createnumismatics/fabric/mixin/PortableItemInterfaceBlockEntityMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public boolean canTransfer() {
101101
}
102102

103103
@Override
104-
public void addBehaviours(List<BlockEntityBehaviour> behaviours) { // no transfer because no entity so sad.
104+
public void addBehaviours(List<BlockEntityBehaviour> behaviours) {
105105
super.addBehaviours(behaviours);
106106
railway$salepointBehaviour = new ItemSalepointTargetBehaviour(this) {
107107
private boolean underControl = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Numismatics
3+
* Copyright (c) 2024 The Railways Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package dev.ithundxr.createnumismatics.content.salepoint.containers.forge;
20+
21+
import dev.ithundxr.createnumismatics.content.salepoint.containers.InvalidatableWrappingItemBuffer;
22+
import net.minecraft.world.SimpleContainer;
23+
import net.minecraft.world.item.ItemStack;
24+
import net.minecraftforge.items.IItemHandlerModifiable;
25+
import org.jetbrains.annotations.NotNull;
26+
27+
public class InvalidatableWrappingItemBufferHandler extends InvalidatableWrappingItemBuffer implements IItemHandlerModifiable {
28+
public InvalidatableWrappingItemBufferHandler(SimpleContainer buffer) {
29+
super(buffer);
30+
}
31+
32+
@Override
33+
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
34+
if (!isValid() || slot >= getSlots())
35+
throw new RuntimeException("Invalid slot");
36+
37+
buffer.setItem(slot, stack);
38+
}
39+
40+
@Override
41+
public int getSlots() {
42+
if (!isValid())
43+
return 0;
44+
45+
return buffer.getContainerSize();
46+
}
47+
48+
@Override
49+
public @NotNull ItemStack getStackInSlot(int slot) {
50+
if (!isValid() || slot >= getSlots())
51+
return ItemStack.EMPTY;
52+
53+
return buffer.getItem(slot);
54+
}
55+
56+
@Override
57+
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
58+
if (!isValid() || slot >= getSlots())
59+
return stack;
60+
61+
int inserted = copyToBufferInternal(stack, simulate);
62+
if (inserted == 0)
63+
return stack;
64+
65+
return stack.copyWithCount(stack.getCount() - inserted);
66+
}
67+
68+
@Override
69+
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
70+
if (!isValid() || slot >= getSlots())
71+
return ItemStack.EMPTY;
72+
73+
if (simulate) {
74+
ItemStack stack = buffer.getItem(slot);
75+
return stack.copyWithCount(Math.min(amount, stack.getCount()));
76+
} else {
77+
return buffer.removeItem(slot, amount);
78+
}
79+
}
80+
81+
@Override
82+
public int getSlotLimit(int slot) {
83+
if (!isValid() || slot >= getSlots())
84+
return 0;
85+
86+
return buffer.getItem(slot).getMaxStackSize();
87+
}
88+
89+
@Override
90+
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
91+
if (!isValid() || slot >= getSlots())
92+
return false;
93+
94+
return buffer.canPlaceItem(slot, stack);
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Numismatics
3+
* Copyright (c) 2024 The Railways Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package dev.ithundxr.createnumismatics.content.salepoint.states.forge;
20+
21+
import dev.ithundxr.createnumismatics.content.salepoint.containers.InvalidatableWrappingItemBuffer;
22+
import dev.ithundxr.createnumismatics.content.salepoint.containers.forge.InvalidatableWrappingItemBufferHandler;
23+
import net.minecraft.world.SimpleContainer;
24+
25+
public class ItemSalepointStateImpl {
26+
public static InvalidatableWrappingItemBuffer createBufferWrapper(SimpleContainer buffer) {
27+
return new InvalidatableWrappingItemBufferHandler(buffer);
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Numismatics
3+
* Copyright (c) 2024 The Railways Team
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package dev.ithundxr.createnumismatics.forge.mixin;
20+
21+
import com.simibubi.create.foundation.item.ItemHandlerWrapper;
22+
import net.minecraftforge.items.IItemHandlerModifiable;
23+
import org.spongepowered.asm.mixin.Mixin;
24+
import org.spongepowered.asm.mixin.gen.Accessor;
25+
26+
@Mixin(ItemHandlerWrapper.class)
27+
public interface ItemHandlerWrapperAccessor {
28+
@Accessor("wrapped")
29+
void setWrapped(IItemHandlerModifiable wrapped);
30+
31+
@Accessor("wrapped")
32+
IItemHandlerModifiable getWrapped();
33+
}

0 commit comments

Comments
 (0)