Skip to content

Commit

Permalink
增加 BundledReg
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Mar 23, 2024
1 parent 1c0c5d3 commit 4fd7fb9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void registerAdditionalModels(ModelEvent.RegisterAdditional event)
SimpleRegistry registry = KasugaLib.STACKS.getRegistries().get(key);
for(ResourceLocation location : registry.model().UNBAKED.keySet()) {
ModelReg reg = registry.model().UNBAKED.get(location);
try{
try {
reg.compileFile(registry);
if(reg.isMultiPart()) {
reg.rebuildAsMultiPart();
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/kasuga/lib/registrations/BundledReg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package kasuga.lib.registrations;

import kasuga.lib.registrations.registry.SimpleRegistry;

import java.util.HashMap;
import java.util.LinkedList;

public class BundledReg<T extends Reg> extends Reg {
private final LinkedList<RegAction<T>> actions;
private final HashMap<String, LinkedList<RegAction<T>>> specificActions;
private final LinkedList<String> elements;
private final HashMap<String, T> regs;
private RegFactory<T> factory = null;
public BundledReg(String registrationKey) {
super(registrationKey);
actions = new LinkedList<>();
specificActions = new HashMap<>();
elements = new LinkedList<>();
regs = new HashMap<>();
}

public BundledReg<T> factory(RegFactory<T> factory) {
this.factory = factory;
return this;
}

public BundledReg<T> element(String registrationKey) {
elements.add(registrationKey);
return this;
}

public BundledReg<T> action(RegAction<T> action) {
actions.add(action);
return this;
}

public BundledReg<T> specificAction(String key, RegAction<T> action) {
if (specificActions.containsKey(key)) {
specificActions.get(key).add(action);
} else {
LinkedList<RegAction<T>> list = new LinkedList<>();
list.add(action);
specificActions.put(key, list);
}
return this;
}

@Override
public BundledReg<T> submit(SimpleRegistry registry) {
if (factory == null) return this;
for (String key : elements) {
T reg = factory.build(key);
for (RegAction<T> action : actions) action.action(reg);

LinkedList<RegAction<T>> specific = specificActions.getOrDefault(key, null);
if (specific != null)
for (RegAction<T> action : specific) action.action(reg);
regs.put(key, (T) reg.submit(registry));
}
return this;
}

public HashMap<String, T> getElements() {
return regs;
}

public T getElement(String key) {
return regs.getOrDefault(key, null);
}

public boolean containsElement(String key) {
return regs.containsKey(key);
}

@Override
public String getIdentifier() {
return "bundled";
}

public interface RegAction<T extends Reg> {
T action(T reg);
}

public interface RegFactory<T extends Reg> {
T build(String registrationKey);
}
}

0 comments on commit 4fd7fb9

Please sign in to comment.