Skip to content

Commit c6540c4

Browse files
committed
Common Storage Lib is having issues with being used for guis. So slowly undoing thsi part. Starting with cooking pot.
1 parent 240fb63 commit c6540c4

File tree

57 files changed

+777
-556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+777
-556
lines changed

common/src/main/java/generations/gg/generations/core/generationscore/common/GenerationsImplementation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface GenerationsImplementation {
3636
* @param log The log block of wood
3737
* @param stripped The stripped log block of wood
3838
*/
39-
fun registerStrippable(log: Holder<Block>, stripped: Holder<Block>)
39+
fun registerStrippable(log: Holder<out Block>, stripped: Holder<out Block>)
4040

4141
/**
4242
* Register a block as flammable.
@@ -68,5 +68,5 @@ interface GenerationsImplementation {
6868

6969
val networkManager: NetworkManager
7070

71-
fun <T: Any> entryRegister(registry: Registry<T>, resourceKey: ResourceKey<Registry<T>>): EntryRegister<T>
71+
fun <T> entryRegister(registry: Registry<T>, resourceKey: ResourceKey<Registry<T>>): EntryRegister<T>
7272
}

common/src/main/java/generations/gg/generations/core/generationscore/common/GenerationsStorage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object GenerationsStorage {
77
val registry = DataManagerRegistry(GenerationsCore.MOD_ID)
88

99
val REGI_ORBS = registry.builder(ItemStorageData.DEFAULT).serialize(ItemStorageData.CODEC).networkSerializer(ItemStorageData.NETWORK_CODEC).buildAndRegister("regi_orbs")
10-
val ITEM_CONTENTS = registry.builder(ItemStorageData.DEFAULT).serialize(ItemStorageData.CODEC).withDataComponent().buildAndRegister("inventory")
10+
val ITEM_CONTENTS = registry.builder(ItemStorageData.DEFAULT).serialize(ItemStorageData.CODEC).networkSerializer(ItemStorageData.NETWORK_CODEC).withDataComponent().buildAndRegister("inventory")
1111
val IMBUED = registry.builder(ItemStorageData.DEFAULT).serialize(ItemStorageData.CODEC).withDataComponent().buildAndRegister("imbued")
1212

1313
fun init() {
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package generations.gg.generations.core.generationscore.common;
2+
3+
import earth.terrarium.common_storage_lib.resources.ResourceStack;
4+
import earth.terrarium.common_storage_lib.resources.item.ItemResource;
5+
import earth.terrarium.common_storage_lib.storage.base.StorageSlot;
6+
import earth.terrarium.common_storage_lib.storage.base.UpdateManager;
7+
import earth.terrarium.common_storage_lib.storage.util.ModifiableItemSlot;
8+
import net.minecraft.world.item.ItemStack;
9+
10+
import java.util.function.Predicate;
11+
12+
public class SimpleItemSlot implements StorageSlot<ItemResource>, ModifiableItemSlot, UpdateManager<ResourceStack<ItemResource>> {
13+
private final Runnable update;
14+
private final Runnable save;
15+
private ItemResource resource;
16+
private long amount;
17+
18+
public SimpleItemSlot(Runnable update, Runnable save) {
19+
this.resource = ItemResource.BLANK;
20+
this.amount = this.getAmount();
21+
this.update = update;
22+
this.save = save;
23+
}
24+
25+
public SimpleItemSlot(Runnable update) {
26+
this(update, () -> {
27+
});
28+
}
29+
30+
public SimpleItemSlot(ItemStack stack) {
31+
this.resource = ItemResource.of(stack);
32+
this.amount = (long)stack.getCount();
33+
this.update = () -> {
34+
};
35+
this.save = () -> {
36+
};
37+
}
38+
39+
public long getLimit(ItemResource resource) {
40+
return resource.isBlank() ? 99L : (long)resource.getCachedStack().getMaxStackSize();
41+
}
42+
43+
public boolean isResourceValid(ItemResource resource) {
44+
return true;
45+
}
46+
47+
public ItemResource getResource() {
48+
return this.resource;
49+
}
50+
51+
public long getAmount() {
52+
return this.amount;
53+
}
54+
55+
public void set(ItemResource resource, long amount) {
56+
this.resource = resource;
57+
this.amount = amount;
58+
this.update();
59+
}
60+
61+
public void set(ResourceStack<ItemResource> data) {
62+
this.resource = (ItemResource)data.resource();
63+
this.amount = data.amount();
64+
this.update();
65+
}
66+
67+
public long insert(ItemResource resource, long amount, boolean simulate) {
68+
if (!this.isResourceValid(resource)) {
69+
return 0L;
70+
} else {
71+
long inserted;
72+
if (this.resource.isBlank()) {
73+
inserted = Math.min(amount, this.getLimit(resource));
74+
if (!simulate) {
75+
this.resource = resource;
76+
this.amount = inserted;
77+
this.save.run();
78+
}
79+
80+
return inserted;
81+
} else if (this.resource.equals(resource)) {
82+
inserted = Math.min(amount, this.getLimit(resource) - this.amount);
83+
if (!simulate) {
84+
this.amount += inserted;
85+
this.save.run();
86+
}
87+
88+
return inserted;
89+
} else {
90+
return 0L;
91+
}
92+
}
93+
}
94+
95+
public long extract(ItemResource resource, long amount, boolean simulate) {
96+
if (this.resource.equals(resource)) {
97+
long extracted = Math.min(amount, this.amount);
98+
if (!simulate) {
99+
this.amount -= extracted;
100+
if (this.amount == 0L) {
101+
this.resource = ItemResource.BLANK;
102+
this.save.run();
103+
}
104+
}
105+
106+
return extracted;
107+
} else {
108+
return 0L;
109+
}
110+
}
111+
112+
public ResourceStack<ItemResource> createSnapshot() {
113+
return new ResourceStack(this.resource, this.amount);
114+
}
115+
116+
public void readSnapshot(ResourceStack<ItemResource> snapshot) {
117+
this.resource = (ItemResource)snapshot.resource();
118+
this.amount = snapshot.amount();
119+
}
120+
121+
public void update() {
122+
this.update.run();
123+
}
124+
125+
public void setAmount(long amount) {
126+
this.amount = amount;
127+
}
128+
129+
public void setResource(ItemResource resource) {
130+
this.resource = resource;
131+
}
132+
133+
public ItemStack toItemStack() {
134+
return this.resource.toStack((int)this.amount);
135+
}
136+
137+
public int getMaxAllowed(ItemResource resource) {
138+
return resource.getCachedStack().getMaxStackSize();
139+
}
140+
141+
public boolean isEmpty() {
142+
return this.resource.isBlank() || this.amount == 0L;
143+
}
144+
145+
public static class Filtered extends SimpleItemSlot {
146+
private final Predicate<ItemResource> filter;
147+
148+
public Filtered(Runnable update, Predicate<ItemResource> filter) {
149+
super(update);
150+
this.filter = filter;
151+
}
152+
153+
public boolean isResourceValid(ItemResource resource) {
154+
return this.filter.test(resource);
155+
}
156+
}
157+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package generations.gg.generations.core.generationscore.common;
2+
3+
import earth.terrarium.common_storage_lib.context.ItemContext;
4+
import earth.terrarium.common_storage_lib.data.DataManager;
5+
import earth.terrarium.common_storage_lib.item.util.ItemStorageData;
6+
import earth.terrarium.common_storage_lib.resources.ResourceStack;
7+
import earth.terrarium.common_storage_lib.resources.item.ItemResource;
8+
import earth.terrarium.common_storage_lib.storage.base.CommonStorage;
9+
import earth.terrarium.common_storage_lib.storage.base.UpdateManager;
10+
import earth.terrarium.common_storage_lib.storage.util.TransferUtil;
11+
import net.minecraft.core.NonNullList;
12+
import net.minecraft.core.component.DataComponentType;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
import java.util.Objects;
16+
import java.util.function.Predicate;
17+
18+
public class SimpleItemStorage implements CommonStorage<ItemResource>, UpdateManager<ItemStorageData> {
19+
protected final NonNullList<SimpleItemSlot> slots;
20+
private final Runnable onUpdate;
21+
private final Runnable save;
22+
23+
public SimpleItemStorage(int size) {
24+
this.slots = NonNullList.withSize(size, new SimpleItemSlot(this::update));
25+
this.onUpdate = () -> {
26+
};
27+
this.save = () -> {
28+
};
29+
}
30+
31+
public SimpleItemStorage(ItemContext context, DataComponentType<ItemStorageData> componentType, int size) {
32+
this.slots = NonNullList.withSize(size, new SimpleItemSlot(this::update));
33+
Objects.requireNonNull(context);
34+
this.onUpdate = context::updateAll;
35+
this.save = () -> {
36+
context.set(componentType, ItemStorageData.of(this));
37+
};
38+
if (context.getResource().has(componentType)) {
39+
this.readSnapshot(context.getResource().get(componentType));
40+
}
41+
42+
}
43+
44+
public SimpleItemStorage(Object entityOrBlockEntity, DataManager<ItemStorageData> dataManager, int size) {
45+
this.slots = NonNullList.withSize(size, new SimpleItemSlot(this::update));
46+
this.onUpdate = () -> {
47+
dataManager.set(entityOrBlockEntity, ItemStorageData.of(this));
48+
};
49+
this.save = () -> {
50+
};
51+
this.readSnapshot(dataManager.get(entityOrBlockEntity));
52+
}
53+
54+
public SimpleItemStorage filter(int slot, Predicate<ItemResource> predicate) {
55+
this.slots.set(slot, new SimpleItemSlot.Filtered(this::update, predicate));
56+
return this;
57+
}
58+
59+
public int size() {
60+
return this.slots.size();
61+
}
62+
63+
public @NotNull SimpleItemSlot get(int index) {
64+
return this.slots.get(index);
65+
}
66+
67+
public ItemStorageData createSnapshot() {
68+
return ItemStorageData.of(this);
69+
}
70+
71+
public void readSnapshot(ItemStorageData snapshot) {
72+
for(int i = 0; i < this.slots.size() && i < snapshot.stacks().size(); ++i) {
73+
((SimpleItemSlot)this.slots.get(i)).readSnapshot((ResourceStack)snapshot.stacks().get(i));
74+
}
75+
76+
}
77+
78+
public void update() {
79+
this.onUpdate.run();
80+
}
81+
82+
public long insert(ItemResource resource, long amount, boolean simulate) {
83+
return TransferUtil.insertSlots(this, resource, amount, simulate);
84+
}
85+
86+
public long extract(ItemResource resource, long amount, boolean simulate) {
87+
return TransferUtil.extractSlots(this, resource, amount, simulate);
88+
}
89+
}

common/src/main/java/generations/gg/generations/core/generationscore/common/client/GenerationsCoreClient.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ import generations.gg.generations.core.generationscore.common.client.render.rare
3030
import generations.gg.generations.core.generationscore.common.client.render.rarecandy.instanceOrNull
3131
import generations.gg.generations.core.generationscore.common.client.screen.container.*
3232
import generations.gg.generations.core.generationscore.common.orFalse
33-
import generations.gg.generations.core.generationscore.common.util.extensions.asValue
3433
import generations.gg.generations.core.generationscore.common.world.container.*
3534
import generations.gg.generations.core.generationscore.common.world.entity.*
3635
import generations.gg.generations.core.generationscore.common.world.item.*
3736
import generations.gg.generations.core.generationscore.common.world.item.MelodyFluteItem.Companion.isItem
3837
import generations.gg.generations.core.generationscore.common.world.item.components.GenerationsDataComponents
39-
import generations.gg.generations.core.generationscore.common.world.item.curry.CurryData
4038
import generations.gg.generations.core.generationscore.common.world.level.block.*
4139
import generations.gg.generations.core.generationscore.common.world.level.block.entities.*
4240
import generations.gg.generations.core.generationscore.common.world.level.block.entities.generic.GenericChestBlockEntity
@@ -52,6 +50,7 @@ import gg.generations.rarecandy.renderer.rendering.RenderStage
5250
import net.minecraft.client.Camera
5351
import net.minecraft.client.Minecraft
5452
import net.minecraft.client.gui.screens.MenuScreens
53+
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen
5554
import net.minecraft.client.model.BoatModel
5655
import net.minecraft.client.model.ChestBoatModel
5756
import net.minecraft.client.model.geom.ModelLayerLocation
@@ -71,12 +70,14 @@ import net.minecraft.client.renderer.entity.ThrownItemRenderer
7170
import net.minecraft.client.renderer.item.ItemProperties
7271
import net.minecraft.core.BlockPos
7372
import net.minecraft.core.Holder
73+
import net.minecraft.network.chat.Component
7474
import net.minecraft.resources.ResourceLocation
7575
import net.minecraft.server.packs.PackType
7676
import net.minecraft.util.Mth
7777
import net.minecraft.world.InteractionHand
7878
import net.minecraft.world.entity.Entity
7979
import net.minecraft.world.entity.EntityType
80+
import net.minecraft.world.entity.player.Inventory
8081
import net.minecraft.world.entity.player.Player
8182
import net.minecraft.world.inventory.AbstractContainerMenu
8283
import net.minecraft.world.inventory.MenuType
@@ -95,6 +96,7 @@ import org.joml.Matrix4f
9596
import org.joml.Vector4f
9697
import java.io.File
9798
import java.util.function.Function
99+
import kotlin.reflect.KFunction3
98100

99101
private operator fun BlockPos.minus(pos: BlockPos): BlockPos {
100102
return this.subtract(pos)
@@ -209,7 +211,7 @@ object GenerationsCoreClient {
209211
ItemProperties.register(
210212
GenerationsItems.CURRY.value(),
211213
GenerationsCore.id("curry_type")
212-
) { itemStack, _, _, _ -> return@register itemStack.get(GenerationsDataComponents.CURRY_DATA.asValue<CurryData>())?.curryType?.ordinal?.let { it / 100f } ?: 0f }
214+
) { itemStack, _, _, _ -> return@register itemStack.get(GenerationsDataComponents.CURRY_DATA.value())?.curryType?.ordinal?.let { it / 100f } ?: 0f }
213215

214216
ItemProperties.register(
215217
GenerationsItems.MELODY_FLUTE.value(),
@@ -297,14 +299,21 @@ object GenerationsCoreClient {
297299
}
298300

299301
fun registerScreens() {
300-
MenuScreens.register(GenerationsContainers.COOKING_POT.asValue<CookingPotContainer>(), ::CookingPotScreen)
301-
MenuScreens.register(GenerationsContainers.GENERIC.asValue<GenericChestContainer<*>>(), ::GenericChestScreen)
302+
registerScreen(GenerationsContainers.COOKING_POT, ::CookingPotScreen)
303+
registerScreen(GenerationsContainers.GENERIC, ::GenericChestScreen)
302304
// MenuRegistry.registerScreenFactory(GenerationsContainers.WALKMON, GenericChestScreen::new);
303305
// MenuRegistry.registerScreenFactory(GenerationsContainers.CALYREX_STEED, GenericChestScreen::new);
304306
// MenuRegistry.registerScreenFactory(GenerationsContainers.MACHINE_BLOCK, ::MachineBlockScreen)
305-
MenuScreens.register(GenerationsContainers.MELODY_FLUTE.asValue<MelodyFluteContainer>(), ::MelodyFluteScreen)
306-
MenuScreens.register(GenerationsContainers.TRASHCAN.asValue<TrashCanContainer>(), ::TrashCanScreen)
307-
MenuScreens.register(GenerationsContainers.RKS_MACHINE.asValue<RksMachineContainer>(), ::RksMachineScreen)
307+
registerScreen(GenerationsContainers.MELODY_FLUTE, ::MelodyFluteScreen)
308+
registerScreen(GenerationsContainers.TRASHCAN, ::TrashCanScreen)
309+
registerScreen(GenerationsContainers.RKS_MACHINE, ::RksMachineScreen)
310+
}
311+
312+
private fun <T: AbstractContainerMenu> registerScreen(
313+
holder: Holder<MenuType<T>>,
314+
function: (T, Inventory, Component) -> AbstractContainerScreen<T>
315+
) {
316+
MenuScreens.register(holder.value(), function)
308317
}
309318

310319
/**

common/src/main/java/generations/gg/generations/core/generationscore/common/client/render/block/entity/CookingPotRenderer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public void render(CookingPotBlockEntity pot, float partialTick, PoseStack stack
3232
stack.pushPose();
3333
stack.translate(0.5f,0.5f, 0.5f);
3434

35-
if(pot.getOuput().isBlank()) {
35+
if(pot.getOuput().isEmpty()) {
3636

37-
renderStack(stack, pot.getIngredient().getCachedStack(), 0.25f, bufferSource, packedLight, packedOverlay);
37+
renderStack(stack, pot.getIngredient(), 0.25f, bufferSource, packedLight, packedOverlay);
3838

3939
for (int i = 0; i < 10; i++) {
40-
ItemStack berry = pot.getBerry(i).getCachedStack();
40+
ItemStack berry = pot.getBerry(i);
4141
stack.pushPose();
4242
stack.mulPose(Axis.YP.rotationDegrees(i * 36));
4343
stack.translate(0.24, 0.03 + -0.001 * i, 0);
4444
renderStack(stack, berry, 0.25f, bufferSource, packedLight, packedOverlay);
4545
stack.popPose();
4646
}
4747
} else {
48-
renderStack(stack, pot.getOuput().getCachedStack(), 0.75f, bufferSource, packedLight, packedOverlay);
48+
renderStack(stack, pot.getOuput(), 0.75f, bufferSource, packedLight, packedOverlay);
4949
}
5050
stack.popPose();
5151

0 commit comments

Comments
 (0)