From 30cef7025e57cbbd66a3a8452b84eeb132b6d8b3 Mon Sep 17 00:00:00 2001 From: kotakotik22 <61428759+kotakotik22@users.noreply.github.com> Date: Mon, 16 Aug 2021 07:57:10 +0300 Subject: [PATCH] Contraption movement setting API - Renamed SpawnerMovementSetting to ContraptionMovementSetting and moved it to its own class - Added a contraption movement setting registry - Made checks previously checking only spawner movement setting and the block being a spawner to check using ContraptionMovementSetting.get(block) --- .../BlockMovementChecks.java | 8 ++- .../mounted/MinecartContraptionItem.java | 16 ++---- .../create/foundation/config/CKinetics.java | 8 +-- .../config/ContraptionMovementSetting.java | 53 +++++++++++++++++++ 4 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/config/ContraptionMovementSetting.java diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementChecks.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementChecks.java index c1b5b796b..81a8b9c0f 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementChecks.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/BlockMovementChecks.java @@ -29,8 +29,8 @@ import com.simibubi.create.content.contraptions.components.structureMovement.pul import com.simibubi.create.content.contraptions.fluids.tank.FluidTankBlock; import com.simibubi.create.content.contraptions.fluids.tank.FluidTankConnectivityHandler; import com.simibubi.create.content.logistics.block.redstone.RedstoneLinkBlock; -import com.simibubi.create.foundation.config.AllConfigs; -import com.simibubi.create.foundation.config.CKinetics; + +import com.simibubi.create.foundation.config.ContraptionMovementSetting; import net.minecraft.block.AbstractPressurePlateBlock; import net.minecraft.block.AbstractRailBlock; @@ -50,7 +50,6 @@ import net.minecraft.block.LadderBlock; import net.minecraft.block.RedstoneDiodeBlock; import net.minecraft.block.RedstoneWallTorchBlock; import net.minecraft.block.RedstoneWireBlock; -import net.minecraft.block.SpawnerBlock; import net.minecraft.block.StandingSignBlock; import net.minecraft.block.TorchBlock; import net.minecraft.block.WallSignBlock; @@ -195,8 +194,7 @@ public class BlockMovementChecks { return false; if (state.getBlock().getTags().contains(NON_MOVABLE)) return false; - if (AllConfigs.SERVER.kinetics.spawnerMovement.get() == CKinetics.SpawnerMovementSetting.UNMOVABLE - && block instanceof SpawnerBlock) + if (ContraptionMovementSetting.get(state.getBlock()) == ContraptionMovementSetting.UNMOVABLE) return false; // Move controllers only when they aren't moving diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java index 1f40b822f..0650302bf 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/mounted/MinecartContraptionItem.java @@ -11,15 +11,13 @@ import com.simibubi.create.AllItems; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity; -import com.simibubi.create.foundation.config.AllConfigs; -import com.simibubi.create.foundation.config.CKinetics; +import com.simibubi.create.foundation.config.ContraptionMovementSetting; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.NBTHelper; import net.minecraft.block.AbstractRailBlock; import net.minecraft.block.BlockState; import net.minecraft.block.DispenserBlock; -import net.minecraft.block.SpawnerBlock; import net.minecraft.block.material.Material; import net.minecraft.dispenser.DefaultDispenseItemBehavior; import net.minecraft.dispenser.IBlockSource; @@ -218,14 +216,10 @@ public class MinecartContraptionItem extends Item { return; OrientedContraptionEntity contraption = (OrientedContraptionEntity) passengers.get(0); - if (AllConfigs.SERVER.kinetics.spawnerMovement.get() == CKinetics.SpawnerMovementSetting.NO_PICKUP) { - Contraption blocks = contraption.getContraption(); - if (blocks != null && blocks.getBlocks().values().stream() - .anyMatch(i -> i.state.getBlock() instanceof SpawnerBlock)) { - player.displayClientMessage(Lang.translate("contraption.minecart_contraption_illegal_pickup") - .withStyle(TextFormatting.RED), true); - return; - } + if(ContraptionMovementSetting.isNoPickup(contraption.getContraption().getBlocks().values())) { + player.displayClientMessage(Lang.translate("contraption.minecart_contraption_illegal_pickup") + .withStyle(TextFormatting.RED), true); + return; } if (event.getWorld().isClientSide) { diff --git a/src/main/java/com/simibubi/create/foundation/config/CKinetics.java b/src/main/java/com/simibubi/create/foundation/config/CKinetics.java index 8739dd8e0..926cefc72 100644 --- a/src/main/java/com/simibubi/create/foundation/config/CKinetics.java +++ b/src/main/java/com/simibubi/create/foundation/config/CKinetics.java @@ -35,8 +35,8 @@ public class CKinetics extends ConfigBase { public ConfigInt maxPistonPoles = i(64, 1, "maxPistonPoles", Comments.maxPistonPoles); public ConfigInt maxRopeLength = i(128, 1, "maxRopeLength", Comments.maxRopeLength); public ConfigInt maxCartCouplingLength = i(32, 1, "maxCartCouplingLength", Comments.maxCartCouplingLength); - public ConfigEnum spawnerMovement = - e(SpawnerMovementSetting.NO_PICKUP, "movableSpawners", Comments.spawnerMovement); + public ConfigEnum spawnerMovement = + e(ContraptionMovementSetting.NO_PICKUP, "movableSpawners", Comments.spawnerMovement); public CStress stressValues = nested(1, CStress::new, Comments.stress); @@ -103,8 +103,4 @@ public class CKinetics extends ConfigBase { ALL, CREEPERS, NONE } - public enum SpawnerMovementSetting { - MOVABLE, NO_PICKUP, UNMOVABLE - } - } diff --git a/src/main/java/com/simibubi/create/foundation/config/ContraptionMovementSetting.java b/src/main/java/com/simibubi/create/foundation/config/ContraptionMovementSetting.java new file mode 100644 index 000000000..3a0c2528d --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/config/ContraptionMovementSetting.java @@ -0,0 +1,53 @@ +package com.simibubi.create.foundation.config; + +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.gen.feature.template.Template; +import net.minecraftforge.common.extensions.IForgeBlock; + +import javax.annotation.Nullable; + +import java.util.Collection; +import java.util.HashMap; +import java.util.function.Supplier; + +public enum ContraptionMovementSetting { + MOVABLE, NO_PICKUP, UNMOVABLE; + + private static HashMap> registry = new HashMap<>(); + + public static void register(ResourceLocation id, Supplier setting) { + registry.put(id, setting); + } + + static { + // config isnt registered at this point, so im using lambda instead of a method reference + register(Blocks.SPAWNER.getRegistryName(), () -> AllConfigs.SERVER.kinetics.spawnerMovement.get()); + } + + @Nullable + public static ContraptionMovementSetting get(Block block) { + if (block instanceof IMovementSettingProvider) + return ((IMovementSettingProvider) block).getContraptionMovementSetting(); + return get(block.getRegistryName()); + } + + @Nullable + public static ContraptionMovementSetting get(ResourceLocation id) { + Supplier supplier = registry.get(id); + return supplier == null ? null : supplier.get(); + } + + protected static boolean allAre(Collection blocks, ContraptionMovementSetting are) { + return blocks.stream().anyMatch(b -> get(b.state.getBlock()) == are); + } + + public static boolean isNoPickup(Collection blocks) { + return allAre(blocks, ContraptionMovementSetting.NO_PICKUP); + } + + public interface IMovementSettingProvider extends IForgeBlock { + ContraptionMovementSetting getContraptionMovementSetting(); + } +}