Search in sources :

Example 1 with SymmetryMirror

use of com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror in project Create by Creators-of-Create.

the class SymmetryWandItem method remove.

public static void remove(Level world, ItemStack wand, Player player, BlockPos pos) {
    BlockState air = Blocks.AIR.defaultBlockState();
    BlockState ogBlock = world.getBlockState(pos);
    checkNBT(wand);
    if (!isEnabled(wand))
        return;
    Map<BlockPos, BlockState> blockSet = new HashMap<>();
    blockSet.put(pos, air);
    SymmetryMirror symmetry = SymmetryMirror.fromNBT((CompoundTag) wand.getTag().getCompound(SYMMETRY));
    Vec3 mirrorPos = symmetry.getPosition();
    if (mirrorPos.distanceTo(Vec3.atLowerCornerOf(pos)) > AllConfigs.SERVER.curiosities.maxSymmetryWandRange.get())
        return;
    symmetry.process(blockSet);
    BlockPos to = new BlockPos(mirrorPos);
    List<BlockPos> targets = new ArrayList<>();
    targets.add(pos);
    for (BlockPos position : blockSet.keySet()) {
        if (!player.isCreative() && ogBlock.getBlock() != world.getBlockState(position).getBlock())
            continue;
        if (position.equals(pos))
            continue;
        BlockState blockstate = world.getBlockState(position);
        if (blockstate.getMaterial() != Material.AIR) {
            targets.add(position);
            world.levelEvent(2001, position, Block.getId(blockstate));
            world.setBlock(position, air, 3);
            if (!player.isCreative()) {
                if (!player.getMainHandItem().isEmpty())
                    player.getMainHandItem().mineBlock(world, blockstate, position, player);
                BlockEntity tileentity = blockstate.hasBlockEntity() ? world.getBlockEntity(position) : null;
                // Add fortune, silk touch and other loot modifiers
                Block.dropResources(blockstate, world, pos, tileentity, player, player.getMainHandItem());
            }
        }
    }
    AllPackets.channel.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> player), new SymmetryEffectPacket(to, targets));
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) Vec3(net.minecraft.world.phys.Vec3) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.core.BlockPos) SymmetryMirror(com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with SymmetryMirror

use of com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror in project Create by Creators-of-Create.

the class SymmetryHandler method render.

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public static void render(RenderLevelLastEvent event) {
    Minecraft mc = Minecraft.getInstance();
    LocalPlayer player = mc.player;
    Random random = new Random();
    for (int i = 0; i < Inventory.getSelectionSize(); i++) {
        ItemStack stackInSlot = player.getInventory().getItem(i);
        if (!AllItems.WAND_OF_SYMMETRY.isIn(stackInSlot))
            continue;
        if (!SymmetryWandItem.isEnabled(stackInSlot))
            continue;
        SymmetryMirror mirror = SymmetryWandItem.getMirror(stackInSlot);
        if (mirror instanceof EmptyMirror)
            continue;
        BlockPos pos = new BlockPos(mirror.getPosition());
        float yShift = 0;
        double speed = 1 / 16d;
        yShift = Mth.sin((float) (AnimationTickHolder.getRenderTime() * speed)) / 5f;
        MultiBufferSource.BufferSource buffer = mc.renderBuffers().bufferSource();
        Camera info = mc.gameRenderer.getMainCamera();
        Vec3 view = info.getPosition();
        PoseStack ms = event.getPoseStack();
        ms.pushPose();
        ms.translate(-view.x(), -view.y(), -view.z());
        ms.translate(pos.getX(), pos.getY(), pos.getZ());
        ms.translate(0, yShift + .2f, 0);
        mirror.applyModelTransform(ms);
        BakedModel model = mirror.getModel().get();
        VertexConsumer builder = buffer.getBuffer(RenderType.solid());
        mc.getBlockRenderer().getModelRenderer().tesselateBlock(player.level, model, Blocks.AIR.defaultBlockState(), pos, ms, builder, true, random, Mth.getSeed(pos), OverlayTexture.NO_OVERLAY, EmptyModelData.INSTANCE);
        buffer.endBatch();
        ms.popPose();
    }
}
Also used : PoseStack(com.mojang.blaze3d.vertex.PoseStack) LocalPlayer(net.minecraft.client.player.LocalPlayer) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) SymmetryMirror(com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror) Minecraft(net.minecraft.client.Minecraft) Random(java.util.Random) Vec3(net.minecraft.world.phys.Vec3) BakedModel(net.minecraft.client.resources.model.BakedModel) EmptyMirror(com.simibubi.create.content.curiosities.symmetry.mirror.EmptyMirror) BlockPos(net.minecraft.core.BlockPos) VertexConsumer(com.mojang.blaze3d.vertex.VertexConsumer) Camera(net.minecraft.client.Camera) ItemStack(net.minecraft.world.item.ItemStack) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 3 with SymmetryMirror

use of com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror in project Create by Creators-of-Create.

the class SymmetryHandler method onClientTick.

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public static void onClientTick(ClientTickEvent event) {
    if (event.phase == Phase.START)
        return;
    Minecraft mc = Minecraft.getInstance();
    LocalPlayer player = mc.player;
    if (mc.level == null)
        return;
    if (mc.isPaused())
        return;
    tickCounter++;
    if (tickCounter % 10 == 0) {
        for (int i = 0; i < Inventory.getSelectionSize(); i++) {
            ItemStack stackInSlot = player.getInventory().getItem(i);
            if (stackInSlot != null && AllItems.WAND_OF_SYMMETRY.isIn(stackInSlot) && SymmetryWandItem.isEnabled(stackInSlot)) {
                SymmetryMirror mirror = SymmetryWandItem.getMirror(stackInSlot);
                if (mirror instanceof EmptyMirror)
                    continue;
                Random r = new Random();
                double offsetX = (r.nextDouble() - 0.5) * 0.3;
                double offsetZ = (r.nextDouble() - 0.5) * 0.3;
                Vec3 pos = mirror.getPosition().add(0.5 + offsetX, 1 / 4d, 0.5 + offsetZ);
                Vec3 speed = new Vec3(0, r.nextDouble() * 1 / 8f, 0);
                mc.level.addParticle(ParticleTypes.END_ROD, pos.x, pos.y, pos.z, speed.x, speed.y, speed.z);
            }
        }
    }
}
Also used : Random(java.util.Random) LocalPlayer(net.minecraft.client.player.LocalPlayer) Vec3(net.minecraft.world.phys.Vec3) EmptyMirror(com.simibubi.create.content.curiosities.symmetry.mirror.EmptyMirror) SymmetryMirror(com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror) ItemStack(net.minecraft.world.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 4 with SymmetryMirror

use of com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror in project Create by Creators-of-Create.

the class SymmetryWandItem method useOn.

@Nonnull
@Override
public InteractionResult useOn(UseOnContext context) {
    Player player = context.getPlayer();
    BlockPos pos = context.getClickedPos();
    if (player == null)
        return InteractionResult.PASS;
    player.getCooldowns().addCooldown(this, 5);
    ItemStack wand = player.getItemInHand(context.getHand());
    checkNBT(wand);
    // Shift -> open GUI
    if (player.isShiftKeyDown()) {
        if (player.level.isClientSide) {
            DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
                openWandGUI(wand, context.getHand());
            });
            player.getCooldowns().addCooldown(this, 5);
        }
        return InteractionResult.SUCCESS;
    }
    if (context.getLevel().isClientSide || context.getHand() != InteractionHand.MAIN_HAND)
        return InteractionResult.SUCCESS;
    CompoundTag compound = wand.getTag().getCompound(SYMMETRY);
    pos = pos.relative(context.getClickedFace());
    SymmetryMirror previousElement = SymmetryMirror.fromNBT(compound);
    // No Shift -> Make / Move Mirror
    wand.getTag().putBoolean(ENABLE, true);
    Vec3 pos3d = new Vec3(pos.getX(), pos.getY(), pos.getZ());
    SymmetryMirror newElement = new PlaneMirror(pos3d);
    if (previousElement instanceof EmptyMirror) {
        newElement.setOrientation((player.getDirection() == Direction.NORTH || player.getDirection() == Direction.SOUTH) ? PlaneMirror.Align.XY.ordinal() : PlaneMirror.Align.YZ.ordinal());
        newElement.enable = true;
        wand.getTag().putBoolean(ENABLE, true);
    } else {
        previousElement.setPosition(pos3d);
        if (previousElement instanceof PlaneMirror) {
            previousElement.setOrientation((player.getDirection() == Direction.NORTH || player.getDirection() == Direction.SOUTH) ? PlaneMirror.Align.XY.ordinal() : PlaneMirror.Align.YZ.ordinal());
        }
        if (previousElement instanceof CrossPlaneMirror) {
            float rotation = player.getYHeadRot();
            float abs = Math.abs(rotation % 90);
            boolean diagonal = abs > 22 && abs < 45 + 22;
            previousElement.setOrientation(diagonal ? CrossPlaneMirror.Align.D.ordinal() : CrossPlaneMirror.Align.Y.ordinal());
        }
        newElement = previousElement;
    }
    compound = newElement.writeToNbt();
    wand.getTag().put(SYMMETRY, compound);
    player.setItemInHand(context.getHand(), wand);
    return InteractionResult.SUCCESS;
}
Also used : CrossPlaneMirror(com.simibubi.create.content.curiosities.symmetry.mirror.CrossPlaneMirror) PlaneMirror(com.simibubi.create.content.curiosities.symmetry.mirror.PlaneMirror) Player(net.minecraft.world.entity.player.Player) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) EmptyMirror(com.simibubi.create.content.curiosities.symmetry.mirror.EmptyMirror) SymmetryMirror(com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror) CrossPlaneMirror(com.simibubi.create.content.curiosities.symmetry.mirror.CrossPlaneMirror) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) Nonnull(javax.annotation.Nonnull)

Example 5 with SymmetryMirror

use of com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror in project Create by Creators-of-Create.

the class SymmetryWandItem method apply.

public static void apply(Level world, ItemStack wand, Player player, BlockPos pos, BlockState block) {
    checkNBT(wand);
    if (!isEnabled(wand))
        return;
    if (!BlockItem.BY_BLOCK.containsKey(block.getBlock()))
        return;
    Map<BlockPos, BlockState> blockSet = new HashMap<>();
    blockSet.put(pos, block);
    SymmetryMirror symmetry = SymmetryMirror.fromNBT((CompoundTag) wand.getTag().getCompound(SYMMETRY));
    Vec3 mirrorPos = symmetry.getPosition();
    if (mirrorPos.distanceTo(Vec3.atLowerCornerOf(pos)) > AllConfigs.SERVER.curiosities.maxSymmetryWandRange.get())
        return;
    if (!player.isCreative() && isHoldingBlock(player, block) && BlockHelper.findAndRemoveInInventory(block, player, 1) == 0)
        return;
    symmetry.process(blockSet);
    BlockPos to = new BlockPos(mirrorPos);
    List<BlockPos> targets = new ArrayList<>();
    targets.add(pos);
    for (BlockPos position : blockSet.keySet()) {
        if (position.equals(pos))
            continue;
        if (world.isUnobstructed(block, position, CollisionContext.of(player))) {
            BlockState blockState = blockSet.get(position);
            for (Direction face : Iterate.directions) blockState = blockState.updateShape(face, world.getBlockState(position.relative(face)), world, position, position.relative(face));
            if (player.isCreative()) {
                world.setBlockAndUpdate(position, blockState);
                targets.add(position);
                continue;
            }
            BlockState toReplace = world.getBlockState(position);
            if (!toReplace.getMaterial().isReplaceable())
                continue;
            if (toReplace.getDestroySpeed(world, position) == -1)
                continue;
            if (AllBlocks.CART_ASSEMBLER.has(blockState)) {
                BlockState railBlock = CartAssemblerBlock.getRailBlock(blockState);
                if (BlockHelper.findAndRemoveInInventory(railBlock, player, 1) == 0)
                    continue;
                if (BlockHelper.findAndRemoveInInventory(blockState, player, 1) == 0)
                    blockState = railBlock;
            } else {
                if (BlockHelper.findAndRemoveInInventory(blockState, player, 1) == 0)
                    continue;
            }
            BlockSnapshot blocksnapshot = BlockSnapshot.create(world.dimension(), world, position);
            FluidState ifluidstate = world.getFluidState(position);
            world.setBlock(position, ifluidstate.createLegacyBlock(), Block.UPDATE_KNOWN_SHAPE);
            world.setBlockAndUpdate(position, blockState);
            CompoundTag wandNbt = wand.getOrCreateTag();
            wandNbt.putBoolean("Simulate", true);
            boolean placeInterrupted = ForgeEventFactory.onBlockPlace(player, blocksnapshot, Direction.UP);
            wandNbt.putBoolean("Simulate", false);
            if (placeInterrupted) {
                blocksnapshot.restore(true, false);
                continue;
            }
            targets.add(position);
        }
    }
    AllPackets.channel.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(() -> player), new SymmetryEffectPacket(to, targets));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) SymmetryMirror(com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) CompoundTag(net.minecraft.nbt.CompoundTag) FluidState(net.minecraft.world.level.material.FluidState)

Aggregations

SymmetryMirror (com.simibubi.create.content.curiosities.symmetry.mirror.SymmetryMirror)5 Vec3 (net.minecraft.world.phys.Vec3)5 BlockPos (net.minecraft.core.BlockPos)4 EmptyMirror (com.simibubi.create.content.curiosities.symmetry.mirror.EmptyMirror)3 ItemStack (net.minecraft.world.item.ItemStack)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2 Minecraft (net.minecraft.client.Minecraft)2 LocalPlayer (net.minecraft.client.player.LocalPlayer)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)2 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)2 PoseStack (com.mojang.blaze3d.vertex.PoseStack)1 VertexConsumer (com.mojang.blaze3d.vertex.VertexConsumer)1 CrossPlaneMirror (com.simibubi.create.content.curiosities.symmetry.mirror.CrossPlaneMirror)1 PlaneMirror (com.simibubi.create.content.curiosities.symmetry.mirror.PlaneMirror)1 Nonnull (javax.annotation.Nonnull)1 Camera (net.minecraft.client.Camera)1