MinionTriggerBlock & More GUI
This commit is contained in:
@@ -3,32 +3,58 @@ package io.github.skippyall.minions.block;
|
||||
import com.mojang.serialization.MapCodec;
|
||||
import eu.pb4.polymer.core.api.block.PolymerBlock;
|
||||
import io.github.skippyall.minions.MinionRegistration;
|
||||
import io.github.skippyall.minions.PlayerClipboardAttachment;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.BlockWithEntity;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityTicker;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.state.StateManager;
|
||||
import net.minecraft.state.property.BooleanProperty;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.hit.BlockHitResult;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.block.WireOrientation;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.nucleoid.packettweaker.PacketContext;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MinionTriggerBlock extends BlockWithEntity implements PolymerBlock {
|
||||
public static final MapCodec<MinionTriggerBlock> CODEC = createCodec(MinionTriggerBlock::new);
|
||||
|
||||
public static final BooleanProperty POWERED = BooleanProperty.of("powered");
|
||||
public static final BooleanProperty RUNNING = BooleanProperty.of("running");
|
||||
|
||||
public MinionTriggerBlock(Settings settings) {
|
||||
super(settings);
|
||||
setDefaultState(getDefaultState().with(POWERED, false));
|
||||
setDefaultState(getDefaultState().with(POWERED, false).with(RUNNING, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
||||
builder.add(POWERED);
|
||||
builder.add(POWERED, RUNNING);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
|
||||
PlayerClipboardAttachment clipboard = player.getAttached(PlayerClipboardAttachment.TYPE);
|
||||
if(clipboard != null) {
|
||||
Optional<MinionTriggerBlockEntity> be = world.getBlockEntity(pos, MinionRegistration.MINION_TRIGGER_BE_TYPE);
|
||||
if(be.isPresent()) {
|
||||
be.get().setInstruction(clipboard.selectedMinion(), clipboard.selectedInstruction());
|
||||
player.playSoundToPlayer(SoundEvents.BLOCK_NOTE_BLOCK_CHIME.value(), SoundCategory.BLOCKS, 1, 1);
|
||||
|
||||
return ActionResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
return ActionResult.PASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,4 +90,13 @@ public class MinionTriggerBlock extends BlockWithEntity implements PolymerBlock
|
||||
public BlockState getPolymerBlockState(BlockState state, PacketContext context) {
|
||||
return state.get(POWERED) ? Blocks.REDSTONE_BLOCK.getDefaultState() : Blocks.GOLD_BLOCK.getDefaultState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
|
||||
if(type == MinionRegistration.MINION_TRIGGER_BE_TYPE) {
|
||||
return MinionTriggerBlockEntity::tick;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,62 +10,80 @@ import net.minecraft.storage.ReadView;
|
||||
import net.minecraft.storage.WriteView;
|
||||
import net.minecraft.util.Uuids;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MinionTriggerBlockEntity extends BlockEntity {
|
||||
private UUID minionUuid;
|
||||
private String instructionName = "";
|
||||
|
||||
private boolean first = true;
|
||||
private boolean runningCache = false;
|
||||
|
||||
public MinionTriggerBlockEntity(BlockPos pos, BlockState state) {
|
||||
super(MinionRegistration.MINION_TRIGGER_BE_TYPE, pos, state);
|
||||
}
|
||||
|
||||
public void updatePower() {
|
||||
boolean powered = getCachedState().get(MinionTriggerBlock.POWERED);
|
||||
ConfiguredInstruction<MinionRuntime> instruction = getInstruction();
|
||||
public void setInstruction(UUID minionUuid, String instructionName) {
|
||||
this.minionUuid = minionUuid;
|
||||
this.instructionName = instructionName;
|
||||
markDirty();
|
||||
}
|
||||
|
||||
if(instruction != null) {
|
||||
if(powered) {
|
||||
instruction.run(getMinion().getInstructionManager());
|
||||
} else {
|
||||
instruction.stop(getMinion().getInstructionManager());
|
||||
public static void tick(World world, BlockPos pos, BlockState state, BlockEntity blockEntity) {
|
||||
if(!(blockEntity instanceof MinionTriggerBlockEntity triggerBlockEntity)) {
|
||||
return;
|
||||
}
|
||||
if(triggerBlockEntity.first) {
|
||||
triggerBlockEntity.first = false;
|
||||
world.updateComparators(pos, MinionRegistration.MINION_TRIGGER_BLOCK);
|
||||
triggerBlockEntity.runningCache = triggerBlockEntity.getInstruction().map(ConfiguredInstruction::isRunning).orElse(false);
|
||||
} else {
|
||||
boolean isRunning = triggerBlockEntity.getInstruction().map(ConfiguredInstruction::isRunning).orElse(false);
|
||||
if (isRunning != triggerBlockEntity.runningCache) {
|
||||
world.updateComparators(pos, MinionRegistration.MINION_TRIGGER_BLOCK);
|
||||
triggerBlockEntity.runningCache = isRunning;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePower() {
|
||||
boolean powered = getCachedState().get(MinionTriggerBlock.POWERED);
|
||||
getMinion().ifPresent(minion -> {
|
||||
getInstruction().ifPresent(instruction -> {
|
||||
if(powered) {
|
||||
instruction.run(minion.getInstructionManager());
|
||||
} else {
|
||||
instruction.stop(minion.getInstructionManager());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public int getComparatorOutput() {
|
||||
ConfiguredInstruction<MinionRuntime> instruction = getInstruction();
|
||||
if(instruction != null && instruction.isRunning()) {
|
||||
Optional<ConfiguredInstruction<MinionRuntime>> instruction = getInstruction();
|
||||
if(instruction.isPresent() && instruction.get().isRunning()) {
|
||||
return 15;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public MinionFakePlayer getMinion() {
|
||||
public Optional<MinionFakePlayer> getMinion() {
|
||||
if(minionUuid != null && world != null && world.getPlayerByUuid(minionUuid) instanceof MinionFakePlayer minion) {
|
||||
return minion;
|
||||
return Optional.of(minion);
|
||||
}
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public ConfiguredInstruction<MinionRuntime> getInstruction() {
|
||||
MinionFakePlayer minion = getMinion();
|
||||
if(minion == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return minion.getInstructionManager().getInstruction(instructionName);
|
||||
public Optional<ConfiguredInstruction<MinionRuntime>> getInstruction(MinionFakePlayer minion) {
|
||||
return Optional.ofNullable(minion.getInstructionManager().getInstruction(instructionName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockReplaced(BlockPos pos, BlockState oldState) {
|
||||
super.onBlockReplaced(pos, oldState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
super.markRemoved();
|
||||
public Optional<ConfiguredInstruction<MinionRuntime>> getInstruction() {
|
||||
return getMinion().flatMap(this::getInstruction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user