Some cleanup

This commit is contained in:
grimmauld 2021-02-20 17:58:16 +01:00
parent a3f2fbe788
commit 6542f2f087
3 changed files with 25 additions and 27 deletions

View File

@ -13,16 +13,17 @@ import net.minecraft.fluid.Fluid;
import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistries;
public class AllTriggers { public class AllTriggers {
private static final List<CriterionTriggerBase<?>> triggers = new LinkedList<>(); private static final List<CriterionTriggerBase<?>> triggers = new LinkedList<>();
public static RegistryTrigger<Fluid> INFINITE_FLUID = add(new RegistryTrigger<>("infinite_fluid", Fluid.class)); public static final RegistryTrigger<Fluid> INFINITE_FLUID = add(new RegistryTrigger<>("infinite_fluid", ForgeRegistries.FLUIDS));
public static RegistryTrigger<Block> BRACKET_APPLY_TRIGGER = add(new RegistryTrigger<>("bracket_apply", Block.class)); public static final RegistryTrigger<Block> BRACKET_APPLY_TRIGGER = add(new RegistryTrigger<>("bracket_apply", ForgeRegistries.BLOCKS));
public static EnumTrigger<InWorldProcessing.Type> FAN_PROCESSING = add(new EnumTrigger<>("fan_processing", InWorldProcessing.Type.class)); public static final EnumTrigger<InWorldProcessing.Type> FAN_PROCESSING = add(new EnumTrigger<>("fan_processing", InWorldProcessing.Type.class));
public static SimpleTrigger public static final SimpleTrigger
ROTATION = simple("rotation"), ROTATION = simple("rotation"),
OVERSTRESSED = simple("overstressed"), OVERSTRESSED = simple("overstressed"),
SHIFTING_GEARS = simple("shifting_gears"), SHIFTING_GEARS = simple("shifting_gears"),
@ -99,9 +100,6 @@ public class AllTriggers {
} }
public static List<ServerPlayerEntity> getPlayersInRange(World world, BlockPos pos, int range) { public static List<ServerPlayerEntity> getPlayersInRange(World world, BlockPos pos, int range) {
List<ServerPlayerEntity> players = return world.getEntitiesWithinAABB(ServerPlayerEntity.class, new AxisAlignedBB(pos).grow(range));
world.getEntitiesWithinAABB(ServerPlayerEntity.class, new AxisAlignedBB(pos).grow(range));
return players;
} }
} }

View File

@ -4,7 +4,6 @@ import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraftforge.registries.RegistryManager;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
@ -14,15 +13,14 @@ import javax.annotation.ParametersAreNonnullByDefault;
public class RegistryTrigger<T extends IForgeRegistryEntry<T>> extends StringSerializableTrigger<T> { public class RegistryTrigger<T extends IForgeRegistryEntry<T>> extends StringSerializableTrigger<T> {
private final IForgeRegistry<T> registry; private final IForgeRegistry<T> registry;
public RegistryTrigger(String id, Class<T> registryType) { public RegistryTrigger(String id, IForgeRegistry<T> registry) {
super(id); super(id);
this.registry = RegistryManager.ACTIVE.getRegistry(registryType); this.registry = registry;
} }
@Nullable @Nullable
@Override @Override
protected T getValue(String key) { protected T getValue(String key) {
return registry.getValue(new ResourceLocation(key)); return registry.getValue(new ResourceLocation(key));
} }

View File

@ -10,7 +10,6 @@ import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@ -18,7 +17,12 @@ import java.util.stream.StreamSupport;
@MethodsReturnNonnullByDefault @MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public abstract class StringSerializableTrigger<T> extends CriterionTriggerBase<StringSerializableTrigger.Instance<T>> { public abstract class StringSerializableTrigger<T> extends CriterionTriggerBase<StringSerializableTrigger.Instance<T>> {
public StringSerializableTrigger(String id) {
protected String getJsonKey() {
return "accepted_entries";
}
protected StringSerializableTrigger(String id) {
super(id); super(id);
} }
@ -32,32 +36,30 @@ public abstract class StringSerializableTrigger<T> extends CriterionTriggerBase<
} }
public ITriggerable constructTriggerFor(@Nullable T entry) { public ITriggerable constructTriggerFor(@Nullable T entry) {
BiConsumer<ServerPlayerEntity, T> trigger = this::trigger; return player -> trigger(player, entry);
return player -> trigger.accept(player, entry);
} }
@Override @Override
public Instance<T> deserializeInstance(JsonObject json, JsonDeserializationContext context) { public Instance<T> deserializeInstance(JsonObject json, JsonDeserializationContext context) {
if (json.has("accepted_entries")) { if (json.has(getJsonKey())) {
JsonArray elements = json.getAsJsonArray("accepted_entries"); JsonArray elements = json.getAsJsonArray(getJsonKey());
return new Instance<>(this, return new Instance<>(this,
StreamSupport.stream(elements.spliterator(), false).map(JsonElement::getAsString) StreamSupport.stream(elements.spliterator(), false).map(JsonElement::getAsString)
.map(rl -> { .map(key -> {
T entry = getValue(rl); T entry = getValue(key);
if (entry == null) if (entry == null)
throw new JsonSyntaxException("Unknown entry '" + rl + "'"); throw new JsonSyntaxException("Unknown entry '" + key + "'");
return entry; return entry;
}).collect(Collectors.toSet())); }).collect(Collectors.toSet()));
} }
return new Instance<>(this, null);
return forEntries((T) null);
} }
@Nullable @Nullable
abstract protected T getValue(String key); protected abstract T getValue(String key);
@Nullable @Nullable
abstract protected String getKey(T value); protected abstract String getKey(T value);
public static class Instance<T> extends CriterionTriggerBase.Instance { public static class Instance<T> extends CriterionTriggerBase.Instance {
@ -84,7 +86,7 @@ public abstract class StringSerializableTrigger<T> extends CriterionTriggerBase<
JsonArray elements = new JsonArray(); JsonArray elements = new JsonArray();
if (entries == null) { if (entries == null) {
jsonobject.add("accepted_entries", elements); jsonobject.add(trigger.getJsonKey(), elements);
return jsonobject; return jsonobject;
} }
@ -96,7 +98,7 @@ public abstract class StringSerializableTrigger<T> extends CriterionTriggerBase<
elements.add(key); elements.add(key);
} }
jsonobject.add("accepted_entries", elements); jsonobject.add(trigger.getJsonKey(), elements);
return jsonobject; return jsonobject;
} }
} }