From 501e0fcf19cb9544fd396340843e8bb75057f64b Mon Sep 17 00:00:00 2001 From: Talrey Date: Mon, 16 Aug 2021 00:44:34 -0700 Subject: [PATCH] Deployer interactor partially implemented, can't swap items yet but can be wrenched --- .../create/AllInteractionBehaviours.java | 15 ++-- .../deployer/DeployerMovingInteraction.java | 72 +++++++++++++++++++ .../MovingInteractionBehaviour.java | 19 +++++ .../LeverMovingInteraction.java | 15 ++-- 4 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovingInteraction.java rename src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/{ => interaction}/LeverMovingInteraction.java (67%) diff --git a/src/main/java/com/simibubi/create/AllInteractionBehaviours.java b/src/main/java/com/simibubi/create/AllInteractionBehaviours.java index 0542bd427..ee99a46f8 100644 --- a/src/main/java/com/simibubi/create/AllInteractionBehaviours.java +++ b/src/main/java/com/simibubi/create/AllInteractionBehaviours.java @@ -1,6 +1,7 @@ package com.simibubi.create; -import com.simibubi.create.content.contraptions.components.structureMovement.LeverMovingInteraction; +import com.simibubi.create.content.contraptions.components.deployer.DeployerMovingInteraction; +import com.simibubi.create.content.contraptions.components.structureMovement.interaction.LeverMovingInteraction; import com.simibubi.create.content.contraptions.components.structureMovement.MovingInteractionBehaviour; import net.minecraft.block.Block; @@ -10,24 +11,25 @@ import net.minecraft.util.ResourceLocation; import javax.annotation.Nullable; import java.util.HashMap; +import java.util.function.Supplier; public class AllInteractionBehaviours { - private static final HashMap INTERACT_BEHAVIOURS = new HashMap<>(); + private static final HashMap> INTERACT_BEHAVIOURS = new HashMap<>(); - public static void addInteractionBehaviour (ResourceLocation loc, MovingInteractionBehaviour behaviour) { + public static void addInteractionBehaviour (ResourceLocation loc, Supplier 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) { + public static void addInteractionBehavioiur (Block block, Supplier behaviour) { addInteractionBehaviour(block.getRegistryName(), behaviour); } @Nullable public static MovingInteractionBehaviour of (ResourceLocation loc) { - return INTERACT_BEHAVIOURS.getOrDefault(loc, null); + return (INTERACT_BEHAVIOURS.get(loc) == null) ? null : INTERACT_BEHAVIOURS.get(loc).get(); } @Nullable @@ -40,6 +42,7 @@ public class AllInteractionBehaviours { } static void register () { - addInteractionBehaviour(Blocks.LEVER.getRegistryName(), new LeverMovingInteraction()); + addInteractionBehaviour(Blocks.LEVER.getRegistryName(), LeverMovingInteraction::new); + addInteractionBehaviour(AllBlocks.DEPLOYER.getId(), DeployerMovingInteraction::new); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovingInteraction.java b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovingInteraction.java new file mode 100644 index 000000000..41e97c6db --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/deployer/DeployerMovingInteraction.java @@ -0,0 +1,72 @@ +package com.simibubi.create.content.contraptions.components.deployer; + +import com.simibubi.create.AllItems; +import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.content.contraptions.components.structureMovement.MovingInteractionBehaviour; + +import com.simibubi.create.foundation.utility.NBTHelper; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.gen.feature.template.Template.BlockInfo; + +import net.minecraft.world.server.ServerWorld; +import net.minecraftforge.common.util.Constants; + +import org.apache.commons.lang3.tuple.MutablePair; + +public class DeployerMovingInteraction extends MovingInteractionBehaviour { + @Override + public boolean handlePlayerInteraction (PlayerEntity player, Hand activeHand, BlockPos localPos, AbstractContraptionEntity contraptionEntity) { + BlockInfo info = contraptionEntity.getContraption().getBlocks().get(localPos); + if (info == null) return false; + MovementContext ctx = null; + int index = -1; + for (MutablePair pair : contraptionEntity.getContraption().getActors()) { + if (info.equals(pair.left)) { + ctx = pair.right; + index = contraptionEntity.getContraption().getActors().indexOf(pair); + break; + } + } + if (ctx == null) return false; + + ItemStack heldStack = player.getItemInHand(activeHand); + // Create.LOGGER.info("<-CTX: " + ctx.data.toString()); + if (heldStack.getItem().equals(AllItems.WRENCH.get())) { + DeployerTileEntity.Mode mode = NBTHelper.readEnum(ctx.tileData, "Mode", DeployerTileEntity.Mode.class); + NBTHelper.writeEnum(ctx.tileData, "Mode", + mode==DeployerTileEntity.Mode.PUNCH ? DeployerTileEntity.Mode.USE : DeployerTileEntity.Mode.PUNCH + ); + // Create.LOGGER.info("Changed mode"); + } else { + // this part isn't quite working yet due to being unable to get the fake player from ctx.temporaryData + DeployerFakePlayer fake = null; + if ( !(ctx.temporaryData instanceof DeployerFakePlayer)) { + if (ctx.world instanceof ServerWorld) { + ctx.temporaryData = new DeployerFakePlayer((ServerWorld) ctx.world); + } + else return false; + } else { + fake = (DeployerFakePlayer)ctx.temporaryData; + } + if (fake == null) return false; + fake.inventory.load(ctx.tileData.getList("Inventory", Constants.NBT.TAG_COMPOUND)); + if (ctx.data.contains("HeldItem")) { + player.setItemInHand(activeHand, ItemStack.of(ctx.data.getCompound("HeldItem"))); + fake.setItemInHand(Hand.MAIN_HAND, heldStack); + } + ctx.tileData.remove("Inventory"); + ctx.temporaryData = fake; + // Create.LOGGER.info("Swapped items"); + } + if (index >= 0) { + // Create.LOGGER.info("->CTX: " + ctx.data.toString()); + setContraptionActorData(contraptionEntity, index, info, ctx); + } + 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 index 717d5f9c8..bf2f9aa3b 100644 --- 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 @@ -1,12 +1,31 @@ package com.simibubi.create.content.contraptions.components.structureMovement; +import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; + import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.gen.feature.template.Template.BlockInfo; + +import org.apache.commons.lang3.tuple.MutablePair; + public abstract class MovingInteractionBehaviour { public MovingInteractionBehaviour () { } + protected void setContraptionActorData (AbstractContraptionEntity contraptionEntity, int index, BlockInfo info, MovementContext ctx) { + contraptionEntity.contraption.actors.remove(index); + contraptionEntity.contraption.actors.add(index, MutablePair.of(info, ctx)); + // mark contraption to re-render because we changed actor data + ContraptionRenderDispatcher.invalidate(contraptionEntity.contraption); + } + + protected void setContraptionBlockData (AbstractContraptionEntity contraptionEntity, BlockPos pos, BlockInfo info) { + contraptionEntity.contraption.blocks.put(pos, info); + // mark contraption to re-render because we changed block data + ContraptionRenderDispatcher.invalidate(contraptionEntity.contraption); + } + public boolean handlePlayerInteraction (PlayerEntity player, Hand activeHand, BlockPos localPos, AbstractContraptionEntity contraptionEntity) { return true; } 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/interaction/LeverMovingInteraction.java similarity index 67% rename from src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/LeverMovingInteraction.java rename to src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/interaction/LeverMovingInteraction.java index 3bb4085bf..f1c901f17 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/LeverMovingInteraction.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/interaction/LeverMovingInteraction.java @@ -1,8 +1,7 @@ -package com.simibubi.create.content.contraptions.components.structureMovement; +package com.simibubi.create.content.contraptions.components.structureMovement.interaction; -import com.simibubi.create.CreateClient; - -import com.simibubi.create.content.contraptions.components.structureMovement.render.ContraptionRenderDispatcher; +import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; +import com.simibubi.create.content.contraptions.components.structureMovement.MovingInteractionBehaviour; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; @@ -11,20 +10,18 @@ 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; +import net.minecraft.world.gen.feature.template.Template.BlockInfo; 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); + BlockInfo info = contraptionEntity.getContraption().getBlocks().get(localPos); BlockState newState = info.state.cycle(BlockStateProperties.POWERED); - contraptionEntity.contraption.blocks.put(localPos, new Template.BlockInfo(info.pos, newState, info.nbt)); + setContraptionBlockData(contraptionEntity, localPos, new 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 ); - // mark contraption to re-render - ContraptionRenderDispatcher.invalidate(contraptionEntity.contraption); return true; } }