Deployer interactor partially implemented, can't swap items yet but can be wrenched
This commit is contained in:
parent
6c3337ae75
commit
501e0fcf19
4 changed files with 106 additions and 15 deletions
|
@ -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<ResourceLocation, MovingInteractionBehaviour> INTERACT_BEHAVIOURS = new HashMap<>();
|
||||
private static final HashMap<ResourceLocation, Supplier<MovingInteractionBehaviour>> INTERACT_BEHAVIOURS = new HashMap<>();
|
||||
|
||||
public static void addInteractionBehaviour (ResourceLocation loc, MovingInteractionBehaviour behaviour) {
|
||||
public static void addInteractionBehaviour (ResourceLocation loc, Supplier<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) {
|
||||
public static void addInteractionBehavioiur (Block block, Supplier<MovingInteractionBehaviour> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<BlockInfo, MovementContext> 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue