Skip to content

Commit 154d834

Browse files
committed
Beta 2.2.6.2545.0
-Added load and save events for FactoryConfig.StorageHandler -Added `put_vec2` UI Definition Element Type -Improved `applyCondition` to work with `children` element type, and when elements from different UI Definitions have the same name -Improved `ListMap` performance by using an internal hash map
1 parent 765e293 commit 154d834

9 files changed

Lines changed: 272 additions & 187 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
org.gradle.jvmargs=-Xmx3072M
22

33
archives_base_name=FactoryAPI
4-
mod_version=2.2.6.2541.3
4+
mod_version=2.2.6.2545.0
55
mod_id=factory_api
66
mod_name=Factory API
77
mod_description=An API that is the basis for mods like Legacy4J, Factocrafty and Better Furnaces Reforged, that work on different mod loaders.

src/main/java/wily/factoryapi/base/client/UIAccessor.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import net.minecraft.world.entity.player.Inventory;
1818
import net.minecraft.world.inventory.Slot;
1919
import net.minecraft.world.item.ItemStack;
20+
import net.minecraft.world.phys.Vec2;
2021
import net.minecraft.world.phys.Vec3;
2122
import org.jetbrains.annotations.Nullable;
2223
import wily.factoryapi.FactoryAPI;
@@ -46,7 +47,7 @@ static UIAccessor of(Gui gui) {
4647
@Nullable
4748
Screen getScreen();
4849

49-
default void reloadUI(){
50+
default void reloadUI() {
5051
beforeInit();
5152
getChildrenRenderables().clear();
5253
afterInit();
@@ -89,29 +90,26 @@ default void beforeInit(UIAccessor accessor) {
8990
getElements().put("inventory.offhand", () -> /*? if >1.21.4 {*//*inventory.getItem(Inventory.SLOT_OFFHAND)*//*?} else {*/inventory.offhand.get(0)/*?}*/);
9091
}
9192
putSupplierComponent("username", () -> Component.literal(Minecraft.getInstance().getUser().getName()));
92-
if (getScreen() instanceof MenuAccess<?> access){
93+
if (getScreen() instanceof MenuAccess<?> access) {
9394
getElements().put("slotsCount", access.getMenu().slots::size);
9495
for (Slot slot : access.getMenu().slots) {
9596
getElements().put("menu.slot." + slot.getContainerSlot(), slot::getItem);
9697
}
9798
}
9899
getDefinitions().clear();
99100
FactoryAPIClient.uiDefinitionManager.applyStatic(accessor);
100-
getStaticDefinitions().stream().filter(d -> d.test(this)).forEach(getDefinitions()::add);
101+
getDefinitions().addAll(getStaticDefinitions());
101102
FactoryAPIClient.uiDefinitionManager.apply(accessor);
102103
UIDefinition.super.beforeInit(accessor);
103104
}
104105

105-
List<UIDefinition> getStaticDefinitions();
106-
107-
108106
default void beforeInit() {
109107
beforeInit(this);
110108
}
111109

112110
default void afterInit() {
113111
afterInit(this);
114-
if (FactoryOptions.UI_DEFINITION_LOGGING.get()){
112+
if (FactoryOptions.UI_DEFINITION_LOGGING.get()) {
115113
FactoryAPI.LOGGER.warn(getElements());
116114
}
117115
}
@@ -195,11 +193,18 @@ default void putSupplierComponent(String name, ArbitrarySupplier<Component> comp
195193
getElements().put(name+".width", component.map(c->Minecraft.getInstance().font.width(c)));
196194
}
197195

198-
default Vec3 putVec3(String name, Vec3 offset) {
196+
default Vec3 putVec3(String name, Vec3 vec3) {
197+
putStaticElement(name, vec3);
198+
putStaticElement(name + ".x", vec3.x());
199+
putStaticElement(name + ".y", vec3.y());
200+
putStaticElement(name + ".z", vec3.z());
201+
return vec3;
202+
}
203+
204+
default Vec2 putVec2(String name, Vec2 offset) {
199205
putStaticElement(name, offset);
200-
putStaticElement(name + ".x", offset.x());
201-
putStaticElement(name + ".y", offset.y());
202-
putStaticElement(name + ".z", offset.z());
206+
putStaticElement(name + ".x", offset.x);
207+
putStaticElement(name + ".y", offset.y);
203208
return offset;
204209
}
205210

@@ -301,6 +306,14 @@ default ResourceLocation getResourceLocation(String name) {
301306
return getResourceLocation(name, null);
302307
}
303308

309+
default ItemStack getItemStack(String name) {
310+
return getElementValue(name, ItemStack.EMPTY, ItemStack.class);
311+
}
312+
313+
default ItemStack getItemStack(String name, ItemStack defaultValue) {
314+
return getElementValue(name, defaultValue, ItemStack.class);
315+
}
316+
304317
default Component getComponent(String name, Component defaultValue) {
305318
return getElementValue(name, defaultValue, Component.class);
306319
}
@@ -322,7 +335,7 @@ default Number getNumber(String name, Number defaultValue) {
322335
return getElementValue(name, defaultValue, Number.class);
323336
}
324337

325-
static UIAccessor createRenderablesWrapper(UIAccessor accessor, List<Renderable> renderables){
338+
static UIAccessor createRenderablesWrapper(UIAccessor accessor, List<Renderable> renderables) {
326339
return new UIAccessor() {
327340
@Override
328341
public @Nullable Screen getScreen() {

src/main/java/wily/factoryapi/base/client/UIDefinition.java

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,90 +7,131 @@
77

88
public interface UIDefinition extends Predicate<UIAccessor> {
99

10-
UIDefinition EMPTY = new UIDefinition(){};
10+
UIDefinition EMPTY = new UIDefinition() {};
1111

12-
default void beforeInit(UIAccessor accessor){
13-
getDefinitions().forEach(d->d.beforeInit(accessor));
12+
default void beforeInit(UIAccessor accessor) {
13+
getDefinitions().removeIf(d -> {
14+
if (d.test(accessor)) {
15+
d.beforeInit(accessor);
16+
return false;
17+
}
18+
return true;
19+
});
1420
}
1521

16-
default void afterInit(UIAccessor accessor){
17-
getDefinitions().forEach(d->d.afterInit(accessor));
22+
default void afterInit(UIAccessor accessor) {
23+
getDefinitions().forEach(d -> d.afterInit(accessor));
1824
}
1925

20-
default void beforeTick(UIAccessor accessor){
21-
getDefinitions().forEach(d->d.beforeTick(accessor));
26+
default void beforeTick(UIAccessor accessor) {
27+
getDefinitions().forEach(d -> d.beforeTick(accessor));
2228
}
2329

24-
default void afterTick(UIAccessor accessor){
25-
getDefinitions().forEach(d->d.afterTick(accessor));
30+
default void afterTick(UIAccessor accessor) {
31+
getDefinitions().forEach(d -> d.afterTick(accessor));
2632
}
2733

28-
default List<UIDefinition> getDefinitions(){
34+
default List<UIDefinition> getDefinitions() {
2935
return Collections.emptyList();
3036
}
3137

38+
default List<UIDefinition> getStaticDefinitions() {
39+
return Collections.emptyList();
40+
}
41+
42+
default void addStatic(UIDefinition uiDefinition) {
43+
getStaticDefinitions().add(uiDefinition);
44+
}
3245

33-
34-
static UIDefinition createBeforeInit(Consumer<UIAccessor> beforeInit){
46+
static UIDefinition createBeforeInit(Consumer<UIAccessor> beforeInit) {
3547
return new UIDefinition() {
3648
@Override
3749
public void beforeInit(UIAccessor accessor) {
38-
UIDefinition.super.beforeInit(accessor);
3950
beforeInit.accept(accessor);
4051
}
4152
};
4253
}
43-
static UIDefinition createBeforeInit(String name, Consumer<UIAccessor> beforeInit){
44-
return createBeforeInit(a->{
45-
if (a.getBoolean(name+".applyCondition",true)) beforeInit.accept(a);
46-
});
54+
55+
@Deprecated
56+
static UIDefinition createBeforeInit(String name, Consumer<UIAccessor> beforeInit) {
57+
return createBeforeInit(beforeInit);
4758
}
48-
static UIDefinition createAfterInit(Consumer<UIAccessor> afterInit){
59+
60+
static UIDefinition createAfterInit(Consumer<UIAccessor> afterInit) {
4961
return new UIDefinition() {
5062
@Override
5163
public void afterInit(UIAccessor accessor) {
52-
UIDefinition.super.afterInit(accessor);
5364
afterInit.accept(accessor);
5465
}
5566
};
5667
}
57-
static UIDefinition createAfterInit(String name, Consumer<UIAccessor> afterInit){
58-
return createAfterInit(a->{
59-
if (a.getBoolean(name+".applyCondition",true)) afterInit.accept(a);
60-
});
68+
69+
@Deprecated
70+
static UIDefinition createAfterInit(String name, Consumer<UIAccessor> afterInit) {
71+
return createAfterInit(afterInit);
6172
}
6273

63-
static UIDefinition createAfterInitWithAmount(String name, Consumer<UIAccessor> afterInit){
64-
return createAfterInit(a->{
74+
static UIDefinition createAfterInitWithAmount(String name, Consumer<UIAccessor> afterInit) {
75+
return createAfterInit(a -> {
6576
Bearer<Integer> bearer = Bearer.of(0);
6677
a.putBearer(name+".index", bearer);
6778
afterInit.accept(a);
6879
});
6980
}
7081

71-
static UIDefinition createBeforeTick(Consumer<UIAccessor> beforeTick){
82+
static UIDefinition createBeforeTick(Consumer<UIAccessor> beforeTick) {
7283
return new UIDefinition() {
7384
@Override
7485
public void beforeTick(UIAccessor accessor) {
75-
UIDefinition.super.beforeTick(accessor);
7686
beforeTick.accept(accessor);
7787
}
7888
};
7989
}
8090

81-
static UIDefinition createAfterTick(Consumer<UIAccessor> afterTick){
91+
static UIDefinition createAfterTick(Consumer<UIAccessor> afterTick) {
8292
return new UIDefinition() {
8393
@Override
8494
public void afterTick(UIAccessor accessor) {
85-
UIDefinition.super.afterTick(accessor);
8695
afterTick.accept(accessor);
8796
}
8897
};
8998
}
9099

91100
@Override
92-
default boolean test(UIAccessor accessor){
101+
default boolean test(UIAccessor accessor) {
93102
return true;
94103
}
95104

105+
106+
class Instance implements UIDefinition {
107+
private final Predicate<UIAccessor> applyCondition;
108+
protected List<UIDefinition> definitions = new ArrayList<>();
109+
protected List<UIDefinition> staticDefinitions = new ArrayList<>();
110+
111+
public Instance(Predicate<UIAccessor> applyCondition) {
112+
this.applyCondition = applyCondition;
113+
}
114+
115+
@Override
116+
public void beforeInit(UIAccessor accessor) {
117+
getDefinitions().clear();
118+
getDefinitions().addAll(getStaticDefinitions());
119+
UIDefinition.super.beforeInit(accessor);
120+
}
121+
122+
@Override
123+
public boolean test(UIAccessor accessor) {
124+
return applyCondition.test(accessor);
125+
}
126+
127+
@Override
128+
public List<UIDefinition> getStaticDefinitions() {
129+
return staticDefinitions;
130+
}
131+
132+
@Override
133+
public List<UIDefinition> getDefinitions() {
134+
return definitions;
135+
}
136+
}
96137
}

0 commit comments

Comments
 (0)