Search in sources :

Example 1 with Type

use of net.minecraft.world.entity.vehicle.AbstractMinecart.Type in project Create by Creators-of-Create.

the class MinecartContraptionItem method wrenchCanBeUsedToPickUpMinecartContraptions.

@SubscribeEvent
public static void wrenchCanBeUsedToPickUpMinecartContraptions(PlayerInteractEvent.EntityInteract event) {
    Entity entity = event.getTarget();
    Player player = event.getPlayer();
    if (player == null || entity == null)
        return;
    ItemStack wrench = player.getItemInHand(event.getHand());
    if (!AllItems.WRENCH.isIn(wrench))
        return;
    if (entity instanceof AbstractContraptionEntity)
        entity = entity.getVehicle();
    if (!(entity instanceof AbstractMinecart))
        return;
    if (!entity.isAlive())
        return;
    AbstractMinecart cart = (AbstractMinecart) entity;
    Type type = cart.getMinecartType();
    if (type != Type.RIDEABLE && type != Type.FURNACE && type != Type.CHEST)
        return;
    List<Entity> passengers = cart.getPassengers();
    if (passengers.isEmpty() || !(passengers.get(0) instanceof OrientedContraptionEntity))
        return;
    OrientedContraptionEntity contraption = (OrientedContraptionEntity) passengers.get(0);
    if (ContraptionMovementSetting.isNoPickup(contraption.getContraption().getBlocks().values())) {
        player.displayClientMessage(Lang.translate("contraption.minecart_contraption_illegal_pickup").withStyle(ChatFormatting.RED), true);
        return;
    }
    if (event.getWorld().isClientSide) {
        event.setCancellationResult(InteractionResult.SUCCESS);
        event.setCanceled(true);
        return;
    }
    ItemStack generatedStack = create(type, contraption).setHoverName(entity.getCustomName());
    try {
        ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
        NbtIo.write(generatedStack.serializeNBT(), dataOutput);
        int estimatedPacketSize = dataOutput.toByteArray().length;
        if (estimatedPacketSize > 2_000_000) {
            player.displayClientMessage(Lang.translate("contraption.minecart_contraption_too_big").withStyle(ChatFormatting.RED), true);
            return;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    player.getInventory().placeItemBackInInventory(generatedStack);
    contraption.discard();
    entity.discard();
    event.setCancellationResult(InteractionResult.SUCCESS);
    event.setCanceled(true);
}
Also used : AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) Entity(net.minecraft.world.entity.Entity) Player(net.minecraft.world.entity.player.Player) Type(net.minecraft.world.entity.vehicle.AbstractMinecart.Type) AbstractMinecart(net.minecraft.world.entity.vehicle.AbstractMinecart) OrientedContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity) ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) IOException(java.io.IOException) ItemStack(net.minecraft.world.item.ItemStack) AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 AbstractContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity)1 OrientedContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.OrientedContraptionEntity)1 IOException (java.io.IOException)1 Entity (net.minecraft.world.entity.Entity)1 Player (net.minecraft.world.entity.player.Player)1 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)1 Type (net.minecraft.world.entity.vehicle.AbstractMinecart.Type)1 ItemStack (net.minecraft.world.item.ItemStack)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1