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);
}
Aggregations