First stab at interactors on contraptions

This commit is contained in:
Talrey 2021-08-08 22:21:48 -07:00
parent 019a61c444
commit 1539f30a3d
6 changed files with 110 additions and 1 deletions

View File

@ -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<ResourceLocation, MovingInteractionBehaviour> 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());
}
}

View File

@ -89,6 +89,7 @@ public class Create {
AllEntityTypes.register();
AllTileEntities.register();
AllMovementBehaviours.register();
AllInteractionBehaviours.register();
AllWorldFeatures.register();
AllEnchantments.register();
AllConfigs.register(ModLoadingContext.get());

View File

@ -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;

View File

@ -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<BlockPos, MountedStorage> storage;
protected Map<BlockPos, MountedFluidStorage> fluidStorage;
protected List<MutablePair<BlockInfo, MovementContext>> actors;
protected Map<BlockPos, MovingInteractionBehaviour> interactors;
protected Set<Pair<BlockPos, Direction>> superglue;
protected List<BlockPos> seats;
protected Map<UUID, Integer> 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<BlockPos, MovingInteractionBehaviour> getInteractors () { return interactors; }
public void updateContainedFluid(BlockPos localPos, FluidStack containedFluid) {
MountedFluidStorage mountedFluidStorage = fluidStorage.get(localPos);
if (mountedFluidStorage != null)

View File

@ -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;
}
}

View File

@ -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;
}
}