diff --git a/src/main/java/com/simibubi/create/AllInteractionBehaviours.java b/src/main/java/com/simibubi/create/AllInteractionBehaviours.java new file mode 100644 index 000000000..0542bd427 --- /dev/null +++ b/src/main/java/com/simibubi/create/AllInteractionBehaviours.java @@ -0,0 +1,45 @@ +package com.simibubi.create; + +import com.simibubi.create.content.contraptions.components.structureMovement.LeverMovingInteraction; +import com.simibubi.create.content.contraptions.components.structureMovement.MovingInteractionBehaviour; + +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.util.ResourceLocation; + +import javax.annotation.Nullable; + +import java.util.HashMap; + +public class AllInteractionBehaviours { + private static final HashMap INTERACT_BEHAVIOURS = new HashMap<>(); + + public static void addInteractionBehaviour (ResourceLocation loc, MovingInteractionBehaviour behaviour) { + if (INTERACT_BEHAVIOURS.containsKey(loc)) { + Create.LOGGER.warn("Interaction behaviour for " + loc.toString() + " was overridden"); + } + INTERACT_BEHAVIOURS.put(loc, behaviour); + } + + public static void addInteractionBehavioiur (Block block, MovingInteractionBehaviour behaviour) { + addInteractionBehaviour(block.getRegistryName(), behaviour); + } + + @Nullable + public static MovingInteractionBehaviour of (ResourceLocation loc) { + return INTERACT_BEHAVIOURS.getOrDefault(loc, null); + } + + @Nullable + public static MovingInteractionBehaviour of (Block block) { + return of(block.getRegistryName()); + } + + public static boolean contains (Block block) { + return INTERACT_BEHAVIOURS.containsKey(block.getRegistryName()); + } + + static void register () { + addInteractionBehaviour(Blocks.LEVER.getRegistryName(), new LeverMovingInteraction()); + } +} diff --git a/src/main/java/com/simibubi/create/Create.java b/src/main/java/com/simibubi/create/Create.java index 6ab5657a9..f239305e3 100644 --- a/src/main/java/com/simibubi/create/Create.java +++ b/src/main/java/com/simibubi/create/Create.java @@ -89,6 +89,7 @@ public class Create { AllEntityTypes.register(); AllTileEntities.register(); AllMovementBehaviours.register(); + AllInteractionBehaviours.register(); AllWorldFeatures.register(); AllEnchantments.register(); AllConfigs.register(ModLoadingContext.get()); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java index f49dea4c7..4dcd0080e 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/AbstractContraptionEntity.java @@ -165,7 +165,8 @@ public abstract class AbstractContraptionEntity extends Entity implements IEntit int indexOfSeat = contraption.getSeats() .indexOf(localPos); if (indexOfSeat == -1) - return false; + return contraption.interactors.containsKey(localPos) + && contraption.interactors.get(localPos).handlePlayerInteraction(player, interactionHand, localPos, this); // Eject potential existing passenger Entity toDismount = null; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java index e36065d43..7a6b98cd8 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/Contraption.java @@ -22,6 +22,8 @@ import java.util.stream.Collectors; import javax.annotation.Nullable; +import com.simibubi.create.AllInteractionBehaviours; + import org.apache.commons.lang3.tuple.MutablePair; import org.apache.commons.lang3.tuple.Pair; @@ -132,6 +134,7 @@ public abstract class Contraption { protected Map storage; protected Map fluidStorage; protected List> actors; + protected Map interactors; protected Set> superglue; protected List seats; protected Map seatMapping; @@ -155,6 +158,7 @@ public abstract class Contraption { storage = new HashMap<>(); seats = new ArrayList<>(); actors = new ArrayList<>(); + interactors = new HashMap<>(); superglue = new HashSet<>(); seatMapping = new HashMap<>(); fluidStorage = new HashMap<>(); @@ -630,6 +634,8 @@ public abstract class Contraption { fluidStorage.put(localPos, new MountedFluidStorage(te)); if (AllMovementBehaviours.contains(captured.state.getBlock())) actors.add(MutablePair.of(blockInfo, null)); + if (AllInteractionBehaviours.contains(captured.state.getBlock())) + interactors.put(localPos, AllInteractionBehaviours.of(captured.state.getBlock())); if (te instanceof CreativeCrateTileEntity && ((CreativeCrateTileEntity) te).getBehaviour(FilteringBehaviour.TYPE) .getFilter() @@ -716,6 +722,12 @@ public abstract class Contraption { NBTHelper.iterateCompoundList(nbt.getList("FluidStorage", NBT.TAG_COMPOUND), c -> fluidStorage .put(NBTUtil.readBlockPos(c.getCompound("Pos")), MountedFluidStorage.deserialize(c.getCompound("Data")))); + interactors.clear(); + NBTHelper.iterateCompoundList(nbt.getList("Interactors", NBT.TAG_COMPOUND), c -> { + BlockPos pos = NBTUtil.readBlockPos(c.getCompound("Pos")); + interactors.put(pos, AllInteractionBehaviours.of(getBlocks().get(pos).state.getBlock())); + }); + if (spawnData) fluidStorage.forEach((pos, mfs) -> { TileEntity tileEntity = presentTileEntities.get(pos); @@ -800,6 +812,13 @@ public abstract class Contraption { fluidStorageNBT.add(c); } + ListNBT interactorNBT = new ListNBT(); + for (BlockPos pos : interactors.keySet()) { + CompoundNBT c = new CompoundNBT(); + c.put("Pos", NBTUtil.writeBlockPos(pos)); + interactorNBT.add(c); + } + nbt.put("Seats", NBTHelper.writeCompoundList(getSeats(), NBTUtil::writeBlockPos)); nbt.put("Passengers", NBTHelper.writeCompoundList(getSeatMapping().entrySet(), e -> { CompoundNBT tag = new CompoundNBT(); @@ -818,6 +837,7 @@ public abstract class Contraption { nbt.put("Blocks", blocksNBT); nbt.put("Actors", actorsNBT); + nbt.put("Interactors", interactorNBT); nbt.put("Superglue", superglueNBT); nbt.put("Storage", storageNBT); nbt.put("FluidStorage", fluidStorageNBT); @@ -1195,6 +1215,8 @@ public abstract class Contraption { return actors; } + public Map getInteractors () { return interactors; } + public void updateContainedFluid(BlockPos localPos, FluidStack containedFluid) { MountedFluidStorage mountedFluidStorage = fluidStorage.get(localPos); if (mountedFluidStorage != null) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/LeverMovingInteraction.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/LeverMovingInteraction.java new file mode 100644 index 000000000..d1b764037 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/LeverMovingInteraction.java @@ -0,0 +1,27 @@ +package com.simibubi.create.content.contraptions.components.structureMovement; + +import com.simibubi.create.CreateClient; + +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.state.properties.BlockStateProperties; +import net.minecraft.util.Hand; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvents; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.gen.feature.template.Template; + +public class LeverMovingInteraction extends MovingInteractionBehaviour { + @Override + public boolean handlePlayerInteraction(PlayerEntity player, Hand activeHand, BlockPos localPos, AbstractContraptionEntity contraptionEntity) { + Template.BlockInfo info = contraptionEntity.contraption.blocks.get(localPos); + BlockState newState = info.state.cycle(BlockStateProperties.POWERED); + contraptionEntity.contraption.blocks.put(localPos, new Template.BlockInfo(info.pos, newState, info.nbt)); + player.getCommandSenderWorld().playSound( + null, player.blockPosition(), SoundEvents.LEVER_CLICK, SoundCategory.BLOCKS, 0.3f, + newState.getValue(BlockStateProperties.POWERED) ? 0.6f : 0.5f + ); + CreateClient.invalidateRenderers(); + return true; + } +} diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovingInteractionBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovingInteractionBehaviour.java new file mode 100644 index 000000000..717d5f9c8 --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovingInteractionBehaviour.java @@ -0,0 +1,13 @@ +package com.simibubi.create.content.contraptions.components.structureMovement; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; + +public abstract class MovingInteractionBehaviour { + public MovingInteractionBehaviour () { } + + public boolean handlePlayerInteraction (PlayerEntity player, Hand activeHand, BlockPos localPos, AbstractContraptionEntity contraptionEntity) { + return true; + } +}