Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warp pipe #87

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
137 changes: 137 additions & 0 deletions src/main/java/fr/hugman/mubble/block/WarpBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package fr.hugman.mubble.block;

import fr.hugman.mubble.block.entity.WarpBlockEntity;
import fr.hugman.mubble.registry.SuperMario;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import java.util.Objects;

/**
* @author MaxBrick
* @since v4.0.0
*/
public class WarpBlock extends Block implements BlockEntityProvider {

public WarpBlock(Settings settings) {
super(settings);
}

@Override
public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) {
return VoxelShapes.union(
VoxelShapes.cuboid(0.0625f, 0.0f, 0.0625f, 0.9375f, 0.125f, 0.9375f),
VoxelShapes.cuboid(0.0625f, 0.0f, 0.0625f, 0.125f, 1.0f, 0.9375f),
VoxelShapes.cuboid(0.0625f, 0.0f, 0.0625f, 0.9375f, 1.0f, 0.125f),
VoxelShapes.cuboid(0.0625f, 0.0f, 0.875f, 0.9375f, 1.0f, 0.9375f),
VoxelShapes.cuboid(0.875f, 0.0f, 0.0625f, 0.9375f, 1.0f, 0.9375f),
VoxelShapes.cuboid(0.0f, 0.625f, 0.0f, 0.125f, 1.0f, 1.0f),
VoxelShapes.cuboid(0.0f, 0.625f, 0.0f, 1.0f, 1.0f, 0.125f),
VoxelShapes.cuboid(0.875f, 0.625f, 0.0f, 1.0f, 1.0f, 1.0f),
VoxelShapes.cuboid(0.0f, 0.625f, 0.875f, 1.0f, 1.0f, 1.0f)
);
}

@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new WarpBlockEntity(pos, state);
}

//Copies coordinates to the Maker Glove if no coordinates are saved
//If coordinates are saved then set destination to glove's coordinates and remove the saved coordinates from glove
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if(!player.getStackInHand(hand).isOf(SuperMario.MAKER_GLOVE)) {
return ActionResult.PASS;
}
if(world.isClient) {
return ActionResult.SUCCESS;
}
if(world.getBlockEntity(pos) instanceof WarpBlockEntity warpBlockEntity && player.getStackInHand(hand).isOf(SuperMario.MAKER_GLOVE)) {
ItemStack itemStack = player.getStackInHand(hand);

if(itemStack.getSubNbt("DestinationPos") == null) {
itemStack.setSubNbt("DestinationPos", NbtHelper.fromBlockPos(pos));

player.sendMessage(Text.of("Copied coordinates to your Maker Glove"), true);

} else if (!NbtHelper.toBlockPos(Objects.requireNonNull(itemStack.getSubNbt("DestinationPos"))).equals(warpBlockEntity.getPos())) {
warpBlockEntity.setDestinationPos(NbtHelper.toBlockPos(Objects.requireNonNull(itemStack.getSubNbt("DestinationPos"))));
warpBlockEntity.markDirty();

itemStack.removeSubNbt("DestinationPos");

player.sendMessage(Text.of("Destination set from your Maker Glove"), true);
}
}
return ActionResult.CONSUME;
}

//Players need to crouch to enter pipe, hence the separate event caller thing
//I don't know how to properly center a location, so I added .5 to x and z
//TODO: Make it so that the entity can step *inside* the block, and ONLY inside the block, to teleport
@Override
public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) {
super.onSteppedOn(world, pos, state, entity);
if (!world.isClient){

BlockEntity blockEntity = world.getBlockEntity(pos);

if (blockEntity instanceof WarpBlockEntity warpBlockEntity){

/*This long "if" statement effectively makes sure the destination block is the corresponding warp block
(in case the destination block is modified for example)
Also, it won't teleport you to the same block (which could soft-lock you)
*/
if(
entity.isPlayer()
&& entity.isInSneakingPose()
&& world.getBlockState(warpBlockEntity.getDestinationPos()).getBlock() == state.getBlock()
&& blockEntity.getPos() != warpBlockEntity.getDestinationPos()
) {
entity.teleport(
warpBlockEntity.getDestinationPos().getX() + 0.5,
warpBlockEntity.getDestinationPos().getY() + 0.126,
warpBlockEntity.getDestinationPos().getZ() + 0.5
);
}
}
}
}

@Override
public void onLandedUpon(World world, BlockState state, BlockPos pos, Entity entity, float fallDistance) {
if (!world.isClient){
super.onLandedUpon(world, state, pos, entity, fallDistance);
BlockEntity blockEntity = world.getBlockEntity(pos);

if (blockEntity instanceof WarpBlockEntity warpBlockEntity){

if(
!entity.isPlayer()
&& world.getBlockState(warpBlockEntity.getDestinationPos()).getBlock() == state.getBlock()
&& blockEntity.getPos() != warpBlockEntity.getDestinationPos()
) {
entity.teleport(
warpBlockEntity.getDestinationPos().getX() + 0.5,
warpBlockEntity.getDestinationPos().getY() + 0.126,
warpBlockEntity.getDestinationPos().getZ() + 0.5
);
}
}
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/fr/hugman/mubble/block/entity/WarpBlockEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.hugman.mubble.block.entity;

import fr.hugman.mubble.registry.SuperMario;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.util.math.BlockPos;

/**
* @author MaxBrick
* @since v4.0.0
*/

public class WarpBlockEntity extends BlockEntity {
private BlockPos destinationPos = pos;

//Copied code from BumpableBlockEntity. Sorry, Hugman X)
public WarpBlockEntity(BlockPos pos, BlockState state) {
super(SuperMario.WARP_BLOCK_ENTITY_TYPE, pos, state);
}

@Override
public void writeNbt(NbtCompound nbt) {
nbt.put("DestinationPos", NbtHelper.fromBlockPos(this.destinationPos));

super.writeNbt(nbt);
}
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);

this.destinationPos = NbtHelper.toBlockPos(nbt.getCompound("DestinationPos"));
}

/*=====================*/
/* GETTERS & SETTERS */
/*=====================*/

public BlockPos getDestinationPos() {
return this.destinationPos;
}
public void setDestinationPos(BlockPos pos) {
this.destinationPos = pos;
}
}
16 changes: 10 additions & 6 deletions src/main/java/fr/hugman/mubble/registry/SuperMario.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import fr.hugman.dawn.block.DawnBlockSettings;
import fr.hugman.dawn.item.DawnItemSettings;
import fr.hugman.mubble.Mubble;
import fr.hugman.mubble.block.BeepBlock;
import fr.hugman.mubble.block.EmptyBlock;
import fr.hugman.mubble.block.DecoratedBumpableBlock;
import fr.hugman.mubble.block.NoteBlock;
import fr.hugman.mubble.block.SnakeBlock;
import fr.hugman.mubble.block.*;
import fr.hugman.mubble.block.entity.BumpableBlockEntity;
import fr.hugman.mubble.block.entity.WarpBlockEntity;
import fr.hugman.mubble.item.CapeFeatherItem;
import fr.hugman.mubble.screen.BumpableBlockScreenHandler;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
Expand Down Expand Up @@ -48,10 +45,13 @@ public class SuperMario {
public static final SnakeBlock SLOW_SNAKE_BLOCK = new SnakeBlock(DawnBlockSettings.copy(Blocks.IRON_BLOCK).mapColor(MapColor.RED).item());
public static final BeepBlock RED_BEEP_BLOCK = new BeepBlock(MapColor.RED, false);
public static final BeepBlock BLUE_BEEP_BLOCK = new BeepBlock(MapColor.BLUE, true);

public static final WarpBlock WARP_PIPE = new WarpBlock(DawnBlockSettings.copy(Blocks.IRON_BLOCK).mapColor(MapColor.GREEN).item());
public static final ScreenHandlerType<BumpableBlockScreenHandler> WARP_BLOCK_SCREEN_HANDLER = new ScreenHandlerType<>(BumpableBlockScreenHandler::new, FeatureFlags.VANILLA_FEATURES);
public static final ScreenHandlerType<BumpableBlockScreenHandler> BUMPABLE_BLOCK_SCREEN_HANDLER = new ScreenHandlerType<>(BumpableBlockScreenHandler::new, FeatureFlags.VANILLA_FEATURES);
public static final BlockEntityType<BumpableBlockEntity> BUMPABLE_BLOCK_ENTITY_TYPE =
FabricBlockEntityTypeBuilder.create(BumpableBlockEntity::new, QUESTION_BLOCK, BRICK_BLOCK, GOLD_BLOCK, NOTE_BLOCK, EXCLAMATION_BLOCK).build();
public static final BlockEntityType<WarpBlockEntity> WARP_BLOCK_ENTITY_TYPE =
FabricBlockEntityTypeBuilder.create(WarpBlockEntity::new, WARP_PIPE).build();
public static final TagKey<Item> CAN_OPEN_BUMPABLE_BLOCKS = DawnFactory.itemTag(Mubble.id("can_open_bumpable_blocks"));

public static final CapeFeatherItem CAPE_FEATHER = new CapeFeatherItem(new Item.Settings(), false);
Expand All @@ -74,6 +74,10 @@ public static void init(Registrar r) {
r.add("slow_snake_block", SLOW_SNAKE_BLOCK);
r.add("red_beep_block", RED_BEEP_BLOCK);
r.add("blue_beep_block", BLUE_BEEP_BLOCK);
r.add("warp_pipe", WARP_PIPE);

Registry.register(Registries.SCREEN_HANDLER, r.id("warp_block"), WARP_BLOCK_SCREEN_HANDLER);
r.add("warp_block", WARP_BLOCK_ENTITY_TYPE);

Registry.register(Registries.SCREEN_HANDLER, r.id("bumpable_block"), BUMPABLE_BLOCK_SCREEN_HANDLER); //TODO: create a registrar method for screen handlers in Dawn API
r.add("bumpable_block", BUMPABLE_BLOCK_ENTITY_TYPE);
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/assets/mubble/blockstates/warp_pipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"variants": {
"": {
"model": "mubble:block/warp_pipe"
}
}
}

146 changes: 146 additions & 0 deletions src/main/resources/assets/mubble/models/block/warp_pipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"textures": {
"down": "mubble:block/warp_pipe/down",
"side": "mubble:block/warp_pipe/side",
"middle": "mubble:block/warp_pipe/middle",
"side_head": "mubble:block/warp_pipe/side_head",
"up": "mubble:block/warp_pipe/up",
"inside": "mubble:block/warp_pipe/inside",
"bottom": "mubble:block/warp_pipe/bottom"

}, "elements": [
{
"from": [1, 0, 1],
"to": [15, 0, 15],
"faces": {
"down": {"uv": [0, 0, 14, 14], "texture": "#down"}
}
}, {
"from": [1, 0, 1],
"to": [15, 10, 1],
"faces": {
"north": {"uv": [0, 0, 14, 10], "texture": "#side"}
}
}, {
"from": [1, 0, 1],
"to": [1, 10, 15],
"faces": {
"west": {"uv": [0, 0, 14, 10], "texture": "#side"}
}
}, {
"from": [15, 0, 1],
"to": [15, 10, 15],
"faces": {
"east": {"uv": [0, 0, 14, 10], "texture": "#side"}
}
}, {
"from": [1, 0, 15],
"to": [15, 10, 15],
"faces": {
"south": {"uv": [0, 0, 14, 10], "texture": "#side"}
}
}, {
"from": [0, 10, 0],
"to": [16, 10, 1],
"faces": {
"down": {"uv": [0, 0, 16, 1], "texture": "#middle"}
}
}, {
"from": [0, 10, 0],
"to": [1, 10, 16],
"faces": {
"down": {"uv": [0, 0, 16, 1], "texture": "#middle"}
}
}, {
"from": [15, 10, 0],
"to": [16, 10, 16],
"faces": {
"down": {"uv": [0, 0, 16, 1], "texture": "#middle"}
}
}, {
"from": [0, 10, 15],
"to": [16, 10, 16],
"faces": {
"down": {"uv": [0, 0, 16, 1], "texture": "#middle"}
}
}, {
"from": [0, 10, 0],
"to": [16, 16, 0],
"faces": {
"north": {"uv": [0, 0, 16, 6], "texture": "#side_head"}
}
}, {
"from": [0, 10, 0],
"to": [0, 16, 16],
"faces": {
"west": {"uv": [0, 0, 16, 6], "texture": "#side_head"}
}
}, {
"from": [16, 10, 0],
"to": [16, 16, 16],
"faces": {
"east": {"uv": [0, 0, 16, 6], "texture": "#side_head"}
}
}, {
"from": [0, 10, 16],
"to": [16, 16, 16],
"faces": {
"south": {"uv": [0, 0, 16, 6], "texture": "#side_head"}
}
}, {
"from": [0, 16, 0],
"to": [2, 16, 16],
"faces": {
"up": {"uv": [0, 0, 16, 2], "texture": "#up"}
}
}, {
"from": [0, 16, 0],
"to": [16, 16, 2],
"faces": {
"up": {"uv": [0, 0, 16, 2], "texture": "#up"}
}
}, {
"from": [14, 16, 0],
"to": [16, 16, 16],
"faces": {
"up": {"uv": [0, 0, 16, 2], "texture": "#up"}
}
}, {
"from": [0, 16, 14],
"to": [16, 16, 16],
"faces": {
"up": {"uv": [0, 0, 16, 2], "texture": "#up"}
}
}, {
"from": [2, 2, 2],
"to": [14, 16, 2],
"faces": {
"south": {"uv": [0, 0, 12, 14], "texture": "#inside"}
}
}, {
"from": [2, 2, 2],
"to": [2, 16, 14],
"faces": {
"east": {"uv": [0, 0, 12, 14], "texture": "#inside"}
}
}, {
"from": [14, 2, 2],
"to": [14, 16, 14],
"faces": {
"west": {"uv": [0, 0, 12, 14], "texture": "#inside"}
}
}, {
"from": [2, 2, 14],
"to": [14, 16, 14],
"faces": {
"north": {"uv": [0, 0, 12, 14], "texture": "#inside"}
}
}, {
"from": [2, 2, 2],
"to": [14, 2, 14],
"faces": {
"up": {"uv": [0, 0, 12, 12], "texture": "#bottom"}
}
}
]
}
4 changes: 4 additions & 0 deletions src/main/resources/assets/mubble/models/item/warp_pipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"parent": "mubble:block/warp_pipe"
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.