use of com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode in project Create by Creators-of-Create.
the class DeployerMovementBehaviour method tick.
@Override
public void tick(MovementContext context) {
if (context.world.isClientSide)
return;
if (!context.stall)
return;
DeployerFakePlayer player = getPlayer(context);
Mode mode = getMode(context);
Pair<BlockPos, Float> blockBreakingProgress = player.blockBreakingProgress;
if (blockBreakingProgress != null) {
int timer = context.data.getInt("Timer");
if (timer < 20) {
timer++;
context.data.putInt("Timer", timer);
return;
}
context.data.remove("Timer");
activate(context, blockBreakingProgress.getKey(), player, mode);
tryDisposeOfExcess(context);
}
context.stall = player.blockBreakingProgress != null;
}
use of com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode in project Create by Creators-of-Create.
the class DeployerHandler method activateInner.
private static void activateInner(DeployerFakePlayer player, Vec3 vec, BlockPos clickedPos, Vec3 extensionVector, Mode mode) {
Vec3 rayOrigin = vec.add(extensionVector.scale(3 / 2f + 1 / 64f));
Vec3 rayTarget = vec.add(extensionVector.scale(5 / 2f - 1 / 64f));
player.setPos(rayOrigin.x, rayOrigin.y, rayOrigin.z);
BlockPos pos = new BlockPos(vec);
ItemStack stack = player.getMainHandItem();
Item item = stack.getItem();
// Check for entities
final ServerLevel world = player.getLevel();
List<Entity> entities = world.getEntitiesOfClass(Entity.class, new AABB(clickedPos)).stream().filter(e -> !(e instanceof AbstractContraptionEntity)).collect(Collectors.toList());
InteractionHand hand = InteractionHand.MAIN_HAND;
if (!entities.isEmpty()) {
Entity entity = entities.get(world.random.nextInt(entities.size()));
List<ItemEntity> capturedDrops = new ArrayList<>();
boolean success = false;
entity.captureDrops(capturedDrops);
// Use on entity
if (mode == Mode.USE) {
InteractionResult cancelResult = ForgeHooks.onInteractEntity(player, entity, hand);
if (cancelResult == InteractionResult.FAIL) {
entity.captureDrops(null);
return;
}
if (cancelResult == null) {
if (entity.interact(player, hand).consumesAction()) {
if (entity instanceof AbstractVillager) {
AbstractVillager villager = ((AbstractVillager) entity);
if (villager.getTradingPlayer() instanceof DeployerFakePlayer)
villager.setTradingPlayer(null);
}
success = true;
} else if (entity instanceof LivingEntity && stack.interactLivingEntity(player, (LivingEntity) entity, hand).consumesAction())
success = true;
}
if (!success && stack.isEdible() && entity instanceof Player) {
Player playerEntity = (Player) entity;
if (playerEntity.canEat(item.getFoodProperties().canAlwaysEat())) {
playerEntity.eat(world, stack);
player.spawnedItemEffects = stack.copy();
success = true;
}
}
}
// Punch entity
if (mode == Mode.PUNCH) {
player.resetAttackStrengthTicker();
player.attack(entity);
success = true;
}
entity.captureDrops(null);
capturedDrops.forEach(e -> player.getInventory().placeItemBackInInventory(e.getItem()));
if (success)
return;
}
// Shoot ray
ClipContext rayTraceContext = new ClipContext(rayOrigin, rayTarget, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player);
BlockHitResult result = world.clip(rayTraceContext);
if (result.getBlockPos() != clickedPos)
result = new BlockHitResult(result.getLocation(), result.getDirection(), clickedPos, result.isInside());
BlockState clickedState = world.getBlockState(clickedPos);
Direction face = result.getDirection();
if (face == null)
face = Direction.getNearest(extensionVector.x, extensionVector.y, extensionVector.z).getOpposite();
// Left click
if (mode == Mode.PUNCH) {
if (!world.mayInteract(player, clickedPos))
return;
if (clickedState.getShape(world, clickedPos).isEmpty()) {
player.blockBreakingProgress = null;
return;
}
LeftClickBlock event = ForgeHooks.onLeftClickBlock(player, clickedPos, face);
if (event.isCanceled())
return;
if (// FIXME: is there an equivalent in world,
BlockHelper.extinguishFire(world, player, clickedPos, face))
// as there was in 1.15?
return;
if (event.getUseBlock() != DENY)
clickedState.attack(world, clickedPos, player);
if (stack.isEmpty())
return;
float progress = clickedState.getDestroyProgress(player, world, clickedPos) * 16;
float before = 0;
Pair<BlockPos, Float> blockBreakingProgress = player.blockBreakingProgress;
if (blockBreakingProgress != null)
before = blockBreakingProgress.getValue();
progress += before;
world.playSound(null, clickedPos, clickedState.getSoundType().getHitSound(), SoundSource.NEUTRAL, .25f, 1);
if (progress >= 1) {
tryHarvestBlock(player, player.gameMode, clickedPos);
world.destroyBlockProgress(player.getId(), clickedPos, -1);
player.blockBreakingProgress = null;
return;
}
if (progress <= 0) {
player.blockBreakingProgress = null;
return;
}
if ((int) (before * 10) != (int) (progress * 10))
world.destroyBlockProgress(player.getId(), clickedPos, (int) (progress * 10));
player.blockBreakingProgress = Pair.of(clickedPos, progress);
return;
}
// Right click
UseOnContext itemusecontext = new UseOnContext(player, hand, result);
Event.Result useBlock = DEFAULT;
Event.Result useItem = DEFAULT;
if (!clickedState.getShape(world, clickedPos).isEmpty()) {
RightClickBlock event = ForgeHooks.onRightClickBlock(player, hand, clickedPos, result);
useBlock = event.getUseBlock();
useItem = event.getUseItem();
}
// Item has custom active use
if (useItem != DENY) {
InteractionResult actionresult = stack.onItemUseFirst(itemusecontext);
if (actionresult != InteractionResult.PASS)
return;
}
boolean holdingSomething = !player.getMainHandItem().isEmpty();
boolean flag1 = !(player.isShiftKeyDown() && holdingSomething) || (stack.doesSneakBypassUse(world, clickedPos, player));
// Use on block
if (useBlock != DENY && flag1 && safeOnUse(clickedState, world, clickedPos, player, hand, result).consumesAction())
return;
if (stack.isEmpty())
return;
if (useItem == DENY)
return;
if (item instanceof BlockItem && !(item instanceof CartAssemblerBlockItem) && !clickedState.canBeReplaced(new BlockPlaceContext(itemusecontext)))
return;
// Reposition fire placement for convenience
if (item == Items.FLINT_AND_STEEL) {
Direction newFace = result.getDirection();
BlockPos newPos = result.getBlockPos();
if (!BaseFireBlock.canBePlacedAt(world, clickedPos, newFace))
newFace = Direction.UP;
if (clickedState.getMaterial() == Material.AIR)
newPos = newPos.relative(face.getOpposite());
result = new BlockHitResult(result.getLocation(), newFace, newPos, result.isInside());
itemusecontext = new UseOnContext(player, hand, result);
}
// 'Inert' item use behaviour & block placement
InteractionResult onItemUse = stack.useOn(itemusecontext);
if (onItemUse.consumesAction())
return;
if (item == Items.ENDER_PEARL)
return;
// buckets create their own ray, We use a fake wall to contain the active area
Level itemUseWorld = world;
if (item instanceof BucketItem || item instanceof SandPaperItem)
itemUseWorld = new ItemUseWorld(world, face, pos);
InteractionResultHolder<ItemStack> onItemRightClick = item.use(itemUseWorld, player, hand);
ItemStack resultStack = onItemRightClick.getObject();
if (resultStack != stack || resultStack.getCount() != stack.getCount() || resultStack.getUseDuration() > 0 || resultStack.getDamageValue() != stack.getDamageValue()) {
player.setItemInHand(hand, onItemRightClick.getObject());
}
CompoundTag tag = stack.getTag();
if (tag != null && stack.getItem() instanceof SandPaperItem && tag.contains("Polishing")) {
player.spawnedItemEffects = ItemStack.of(tag.getCompound("Polishing"));
AllSoundEvents.SANDING_SHORT.playOnServer(world, pos, .25f, 1f);
}
if (!player.getUseItem().isEmpty())
player.setItemInHand(hand, stack.finishUsingItem(world, player));
player.stopUsingItem();
}
use of com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode in project Create by Creators-of-Create.
the class DeployerMovementBehaviour method visitNewPosition.
@Override
public void visitNewPosition(MovementContext context, BlockPos pos) {
if (context.world.isClientSide)
return;
tryGrabbingItem(context);
DeployerFakePlayer player = getPlayer(context);
Mode mode = getMode(context);
if (mode == Mode.USE && !DeployerHandler.shouldActivate(player.getMainHandItem(), context.world, pos, null))
return;
activate(context, pos, player, mode);
tryDisposeOfExcess(context);
context.stall = player.blockBreakingProgress != null;
}
use of com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode in project Create by Creators-of-Create.
the class DeployerRenderer method renderInContraption.
public static void renderInContraption(MovementContext context, VirtualRenderWorld renderWorld, ContraptionMatrices matrices, MultiBufferSource buffer) {
VertexConsumer builder = buffer.getBuffer(RenderType.solid());
BlockState blockState = context.state;
Mode mode = NBTHelper.readEnum(context.tileData, "Mode", Mode.class);
PartialModel handPose = getHandPose(mode);
float speed = (float) context.getAnimationSpeed();
if (context.contraption.stalled)
speed = 0;
SuperByteBuffer shaft = CachedBufferer.block(AllBlocks.SHAFT.getDefaultState());
SuperByteBuffer pole = CachedBufferer.partial(AllBlockPartials.DEPLOYER_POLE, blockState);
SuperByteBuffer hand = CachedBufferer.partial(handPose, blockState);
double factor;
if (context.contraption.stalled || context.position == null || context.data.contains("StationaryTimer")) {
factor = Mth.sin(AnimationTickHolder.getRenderTime() * .5f) * .25f + .25f;
} else {
Vec3 center = VecHelper.getCenterOf(new BlockPos(context.position));
double distance = context.position.distanceTo(center);
double nextDistance = context.position.add(context.motion).distanceTo(center);
factor = .5f - Mth.clamp(Mth.lerp(AnimationTickHolder.getPartialTicks(), distance, nextDistance), 0, 1);
}
Vec3 offset = Vec3.atLowerCornerOf(blockState.getValue(FACING).getNormal()).scale(factor);
PoseStack m = matrices.getModel();
m.pushPose();
m.pushPose();
Axis axis = Axis.Y;
if (context.state.getBlock() instanceof IRotate) {
IRotate def = (IRotate) context.state.getBlock();
axis = def.getRotationAxis(context.state);
}
float time = AnimationTickHolder.getRenderTime(context.world) / 20;
float angle = (time * speed) % 360;
TransformStack.cast(m).centre().rotateY(axis == Axis.Z ? 90 : 0).rotateZ(axis.isHorizontal() ? 90 : 0).unCentre();
shaft.transform(m);
shaft.rotateCentered(Direction.get(AxisDirection.POSITIVE, Axis.Y), angle);
m.popPose();
m.translate(offset.x, offset.y, offset.z);
pole.transform(m);
hand.transform(m);
transform(pole, blockState, true);
transform(hand, blockState, false);
shaft.light(matrices.getWorld(), ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)).renderInto(matrices.getViewProjection(), builder);
pole.light(matrices.getWorld(), ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)).renderInto(matrices.getViewProjection(), builder);
hand.light(matrices.getWorld(), ContraptionRenderDispatcher.getContraptionWorldLight(context, renderWorld)).renderInto(matrices.getViewProjection(), builder);
m.popPose();
}
Aggregations