mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 14:52:58 +00:00
Code cleanup (#5468)
* remove direct guava predicates from BlockStateMachine * cleanup blockstatebasic * fix a large number of e.printStacktrace calls, narrow ComputerIntegration exception signature * fix simple todos, remove invalid todos, unify some duplicate rendering code * use a generic method to get around unchecked warning * remove some redundant uses of hasCapability * experimental OptionalCapability class. not in use yet * nullable checks/annotations & remove duplicate field controlType in factory TE * Inline various redundant variables * Remove unused list * Cleanup BlockStateGenerator * Replace string constants with pointers to the proper mod id constant * As 9.7.5 breaks backwards compat API wise for the DynamicNetwork this is as good a time as any to remove this deprecated method * Use two argument constructor instead of appending a ':' inbetween * Convert other BlockState type classes to using suppliers for tile entity creation
This commit is contained in:
1 parent
40620ec23b
commit
80b07bc4b3
115 files changed
+583
-633
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package mekanism.common.util;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
@@ -19,4 +20,80 @@ public final class CapabilityUtils {
|
||||
}
|
||||
return provider.getCapability(cap, side);
|
||||
}
|
||||
|
||||
public static <T> OptionalCapability withCapability(ICapabilityProvider provider, Capability<T> cap, EnumFacing side, Consumer<T> consumer) {
|
||||
return new OptionalCapability(provider).orElseWith(cap, side, consumer);
|
||||
}
|
||||
|
||||
public static <T> OptionalCapability withCapability(boolean testResult, ICapabilityProvider provider, Capability<T> cap, EnumFacing side, Consumer<T> consumer) {
|
||||
return new OptionalCapability(provider).orElseWith(testResult, cap, side, consumer);
|
||||
}
|
||||
|
||||
public static class OptionalCapability {
|
||||
|
||||
private final ICapabilityProvider provider;
|
||||
|
||||
OptionalCapability(ICapabilityProvider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* If this chain has not yet been satisfied, try to get the capability and consume it.
|
||||
*
|
||||
* @param capability the cap to check
|
||||
* @param side side to give to the provider
|
||||
* @param consumer function to use the capability
|
||||
* @param <T> the capability type
|
||||
*
|
||||
* @return this instance if unsuccessful, otherwise a no-op OptionalCapability
|
||||
*/
|
||||
public <T> OptionalCapability orElseWith(Capability<T> capability, EnumFacing side, Consumer<T> consumer) {
|
||||
T cap = this.provider.getCapability(capability, side);
|
||||
if (cap != null) {
|
||||
consumer.accept(cap);
|
||||
return NoOpOptionalCapability.INSTANCE;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #orElseWith(Capability, EnumFacing, Consumer)}, but skips if boolean testResult fails.
|
||||
*
|
||||
* @param testResult if this param is false, cap is not processed
|
||||
* @param capability the cap to check
|
||||
* @param side side to give to the provider
|
||||
* @param consumer function to use the capability
|
||||
* @param <T> the capability type
|
||||
*
|
||||
* @return this instance if unsuccessful (or testResult is false), otherwise a no-op OptionalCapability
|
||||
*/
|
||||
public <T> OptionalCapability orElseWith(boolean testResult, Capability<T> capability, EnumFacing side, Consumer<T> consumer) {
|
||||
if (!testResult) {
|
||||
return this;
|
||||
}
|
||||
return orElseWith(capability, side, consumer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All method should be no-op version of super, return this instance when the action succeeded
|
||||
*/
|
||||
public static class NoOpOptionalCapability extends OptionalCapability {
|
||||
|
||||
public static final NoOpOptionalCapability INSTANCE = new NoOpOptionalCapability();
|
||||
|
||||
private NoOpOptionalCapability() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> OptionalCapability orElseWith(Capability<T> capability, EnumFacing side, Consumer<T> consumer) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> OptionalCapability orElseWith(boolean testResult, Capability<T> capability, EnumFacing side, Consumer<T> consumer) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user