Command Block (Khối Lệnh) là một trong những công cụ mạnh mẽ và thay đổi gameplay nhất trong Minecraft Java Edition (PC). Khác với các phiên bản Bedrock, phiên bản Java cho phép truy cập sâu vào các lệnh /execute, /data, /function và hệ thống datapack, mở ra khả năng tự động hóa, tạo map adventure, minigame hay thậm chí là các mod “vanilla” phức tạp mà không cần cài đặt mod bên thứ ba. Bài viết này sẽ cung cấp một hướng dẫn informational (thông tin, kiến thức) đầy đủ, chi tiết và có cấu trúc để bạn làm chủ Command Block trên Minecraft PC.
1. Command Block Là Gì? Tại Sao Phải Dùng Trên PC (Java Edition)?
1.1. Định Nghĩa Cơ Bản
Command Block là một khối đặc biệt (không có trong menu Creative mặc định) cho phép thực thi các lệnh console (console commands) khi được kích hoạt bởi tín hiệu Redstone hoặc các điều kiện khác. Nó đóng vai trò như “bộ não” của các hệ thống tự động trong game.
1.2. Đặc Biệt Của Phiên Bản Java Edition (PC)
Mặc dù Bedrock cũng có Command Block, nhưng Java Edition nổi bật với:
Cú pháp lệnh chuẩn NBT/JSON: Hỗ trợ đầy đủ nbt={...}, components={...} (Text Component), predicates.
Hệ thống Datapack/Function: Cho phép viết code ngoài game (bằng file .mcfunction) và load vào, quản lý version bằng Git, debug dễ dàng hơn nhồi nhét vào GUI Command Block.
Lệnh /execute mạnh mẽ: Cú pháp /execute mới (từ 1.13+) cho phép chuỗi điều kiện (subcommands) như if, unless, as, at, positioned, rotated, store… cực kỳ linh hoạt.
Không giới hạn độ dài ký tự trong Function file: Command Block GUI giới hạn ~32.500 ký tự, nhưng file function gần như không giới hạn.
2. Cách Lấy Command Block Trên Minecraft PC
Vì là khối “technical”, bạn không tìm thấy nó trong Creative Inventory.
2.1. Điều Kiện Tiên Quyết
- Cheats phải được bật: Khi tạo thế giới mới -> More World Options -> Allow Cheats: ON. Hoặc nếu đã có world: Mở LAN -> Allow Cheats: ON -> Start LAN World.
- Quyền Operator (OP): Trên server, bạn cần cấp quyền OP cho mình (
/op <tên_người_chơi>). Trên singleplayer, bạn mặc định là OP nếu cheats bật.
2.2. Lệnh Lấy Khối
Mở chat (phím T hoặc /) và gõ:
/give @p command_block
@p: Target selector chọn người chơi gần nhất (bạn).- Kết quả: 1 Command Block xuất hiện trong inventory.
Mẹo: Dùng
/give @p command_block 64để lấy một stack đầy.
3. Giao Diện GUI Command Block: Phân Tích Chi Tiết
Khi đặt xuống và chuột phải vào Command Block, bạn thấy 3 tab chính (trên Java Edition 1.13+): Impulse/Chain/Repeat, Unconditional/Conditional, Always Active/Needs Redstone. Hiểu rõ 3 cặp này là chìa khóa.
3.1. Loại Khối (Block Type) – 3 Chế Độ Cơ Bản
- Impulse (Màu Cam/Đỏ): Chạy lệnh một lần khi nhận được tín hiệu Redstone (rising edge). Mặc định.
- Chain (Màu Xanh Dương/Green): Chỉ chạy nếu khối Command Block phía sau (mũi tên chỉ vào nó) đã chạy thành công. Dùng để tạo chuỗi lệnh tuần tự.
- Repeat (Màu Tím/Xanh Lam): Chạy lệnh liên tục (mỗi tick – 20 lần/giây) miễn là điều kiện kích hoạt được thỏa mãn. Dùng cho loop, detector, clock.
3.2. Điều Kiện Thực Thi (Condition)

Có thể bạn quan tâm: Toàn Tập Hướng Dẫn Lệnh Bay Trong Minecraft: Từ Cơ Bản Đến Nâng Cao, Tất Cả Những Gì Bạn Cần Biết
- Unconditional (Mặc định): Luôn chạy lệnh khi được kích hoạt, không quan tâm khối phía sau.
- Conditional: Chỉ chạy lệnh nếu khối Command Block phía sau (theo hướng mũi tên) đã thực thi thành công (Success Count > 0). Rất quan trọng cho logic
if/else.
3.3. Chế Độ Kích Hoạt (Redstone Mode)
- Needs Redstone (Mặc định): Cần tín hiệu Redstone mạnh (Redstone dust, Repeater, Lever, Block…) mới chạy.
- Always Active: Tự chạy liên tục (Repeat) hoặc tự chạy một lần khi đặt/load chunk (Impulse/Chain) mà không cần Redstone.
- Lưu ý: Repeat + Always Active = Redstone Clock 20Hz chuẩn, hiệu năng tốt hơn Redstone dust.
3.4. Các Ô Nhập Liệu Quan Trọng Khác
- Command Input: Nơi gõ lệnh (Giới hạn 32.500 ký tự).
- Previous Output: Hiển thị kết quả trả về của lần chạy trước (Success/Error message). Có nút
Ođể bật/tắt hiển thị ở góc dưới bên phải màn hình. - Track Output: Nếu tắt, Previous Output sẽ không cập nhật (giúp giảm lag nhẹ nếu command block spam output).
- Custom Name: Đặt tên cho khối (hiện khi trỏ chuột), hữu ích để debug trong danh sách
/data get block <pos>.
4. Nền Tảng Kiến Thức: Target Selectors (Bộ Chọn Đối Tượng) & Toạ Độ
Trước khi viết lệnh phức tạp, bạn bắt buộc thành thạo Selector (@p, @a, @e, @r, @s) và Arguments.
4.1. 5 Selector Cơ Bản
| Selector | Ý Nghĩa | Dùng Khi Nào |
|---|---|---|
@p |
Nearest Player (Người chơi gần nhất) | Tặng item, teleport người chơi kích hoạt. |
@a |
All Players (Tất cả người chơi) | Broadcast message, effect toàn server. |
@r |
Random Player (Ngẫu nhiên) | Minigame chọn ngẫu nhiên. |
@e |
All Entities (Tất cả thực thể) | Kill mob, summon, modify data mob/item frame/armor stand. |
@s |
Self (Chính thực thể đang chạy lệnh) | Quan trọng nhất trong /execute as ... |
4.2. Arguments (Tham Số Lọc) – Cú Pháp: @earg=value,arg=value
distance=..10/distance=5../distance=5..10: Khoảng cách (blocks)...10= <=10.5..= >=5.tag=!mytag/tag=mytag: Lọc bằng Tag (Tag là string tùy ý, ví dụtag=boss,tag=!player).type=minecraft:zombie/type=!minecraft:player: Lọc theo loại thực thể.name="Tên",nbt={CustomName:'{"text":"Tên"}'}: Lọc theo tên hiển thị hoặc NBT data.scores={obj=10..}: Lọc theo Scoreboard.limit=1: Giới hạn số lượng kết quả (mặc định @p/@r=1, @a/@e=unlimited).sort=nearest/furthest/random/arbitrary: Sắp xếp kết quả.
4.3. Toạ Độ Tương Đối (Relative Coordinates) ^ ^ ^ vs ~ ~ ~
Đây là điểm gây nhầm lẫn nhiều nhất trên PC:
~ ~ ~ (Tilde): Tương đối so với vị trí thực thi (execution position), KHÔNG xoay theo hướng nhìn.
~5 ~ ~ = 5 block về phía Dương trục X (East), bất kể bạn nhìn đâu.
^ ^ ^ (Caret): Tương đối so với vị trí VÀ hướng nhìn (rotation) của thực thể thực thi.
^ ^ ^5 = 5 block phía trước mặt (Forward).
^ ^5 ^ = 5 block phía trên đầu (Up – theo pitch).
^5 ^ ^ = 5 block phía bên trái (Left).
Quy tắc vàng: Dùng
~ ~ ~cho toạ độ thế giới (world coords) hoặc vị trí cố định. Dùng^ ^ ^cho vị trí tương đối theo hướng nhìn (spawn particle trước mặt, shoot projectile).
5. Lệnh /execute: “Trái Tim” Của Command Block Hiện Đại (1.13+)
Nếu chỉ dùng /give, /tp, /say thì Command Block chỉ ở trình độ cơ bản. /execute mới cho phép lập trình logic thực sự.
5.1. Cấu Trúc Chuẩn
/execute <subcommand1> <subcommand2> ... run <command>
Các subcommand thực thi tuần tự từ trái sang phải, thay đổi “Execution Context” (Ngữ cảnh thực thi: Vị trí, Hướng, Thực thể, Dimension, Điều kiện…).
5.2. Các Subcommand Phổ Biến Nhất
| Subcommand | Chức Năng | Ví Dụ |
|---|---|---|
as <selector> |
Thay đổi thực thể thực thi (@s) thành selector. Giữ nguyên vị trí/hướng. |
as @atag=admin |
at <selector> |
Thay đổi vị trí & hướng thực thi thành vị trí/hướng của selector. Giữ nguyên @s. |
at @s (dùng sau as để @s vừa là actor vừa là origin) |
positioned <x y z> / positioned as <selector> |
Đặt vị trí thực thi cố định hoặc theo entity khác. | positioned ~ ~5 ~ |
rotated <yaw pitch> / rotated as <selector> |
Đặt góc quay thực thi. | rotated as @p |
in <dimension> |
Thay đổi dimension thực thi (minecraft:overworld, the_nether, the_end). |
in minecraft:the_nether |
if <condition> / unless <condition> |
Điều kiện dừng (Short-circuit). Nếu false -> dừng hẳn, không chạy run. |
if block ~ ~-1 ~ minecraft:diamond_block |
store result/success <target> <type> <scale> |
Lưu kết quả (result) hoặc thành công (0/1) vào scoreboard, block data, bossbar… | store result score @s money run ... |
align <xyz> |
Căn chỉnh vị trí thực thi về block nguyên (integer). | align xyz |
5.3. Ví Dụ Thực Chiến: “Ném Cúi Cây Tự Động” (Tree Feller)

Có thể bạn quan tâm: Hướng Dẫn Chi Tiết Về Các Lệnh Tạo Hiệu Ứng Trong Minecraft
Logic: Khi cầm Rìu (Diamond Axe) + Sneak + Click chuột phải vào Gỗ -> Phá toàn bộ cây.
Setup: Scoreboard detect minecraft.used:minecraft.diamond_axe.
Command Block Chain (Always Active):
-
Repeat/Always Active: Detect player dùng rìu.
mcfunction execute as @ascores={used_axe=1..} at @s anchored eyes positioned ^ ^ ^5 if block ~ ~ ~ #minecraft:logs run function mypack:tree_feller
Giải thích:as @a->@slà player.at @s anchored eyes-> origin ở mắt player.positioned ^ ^ ^5-> check block cách 5 block trước mặt.if block ... logs-> nếu là gỗ thì run function. -
Function
tree_feller(Đệ quy hoặc Loop):
Dùng/executeloop hoặc function đệ quy để phá block liên kết. Đây là lúc sức mạnh của/execute+ function file thể hiện rõ ràng hơn nhồi nhét vào GUI.
6. Hệ Thống Datapack & Function: Vượt Qua Giới Hạn GUI
Trên PC (Java Edition), viết lệnh trực tiếp vào GUI Command Block là thói quen xấu cho project lớn.
Khó debug: Không có syntax highlight, không search/replace, không version control.
Giới hạn ký tự: 32.500 chars.
Lag khi edit: Mở GUI khối lớn gây lag client.
6.1. Cấu Trúc Datapack Chuẩn
world/datapacks/my_pack/
├── pack.mcmeta (Metadata: description, pack_format)
└── data/ └── my_namespace/ ├── functions/ │ ├── load.mcfunction (Chạy 1 lần khi load/reload) │ ├── tick.mcfunction (Chạy mỗi tick - gắn vào #minecraft:tick) │ └── main.mcfunction (Logic chính) ├── tags/ │ └── functions/ │ ├── load.json (List hàm chạy khi load) │ └── tick.json (List hàm chạy mỗi tick) └── predicates/... (Điều kiện tái sử dụng)
6.2. Lệnh Quản Lý Datapack In-Game
/datapack list: Xem danh sách./datapack enable "file/my_pack": Bật pack./reload: Lệnh quan trọng nhất. Reload resource pack, datapack, advancement, predicate, tag… mà không cần thoát world. Dùng liên tục khi dev.
6.3. Ưu Điểm Của Function File
- Comment: Dùng
# Dòng commentđể ghi chú code. - Đệ quy (Recursion): Function A gọi Function A (với điều kiện dừng) -> Xử lý cây, flood fill, pathfinding.
- Schedule:
/schedule function my:func 10t/1s/1d-> Delay chính xác không cần Redstone clock. - Performance: Game parse function file 1 lần khi load, nhanh hơn parse chuỗi string trong Command Block mỗi tick.
7. NBT Data & Data Components (1.20.5+): Sửa Đổi Sâu Đồ Vật
Trên PC Java Edition, việc thao tác NBT là đẳng cấp cao nhất.
7.1. Cú Pháp NBT Cơ Bản
{Key:Value, List:Item1, Item2, Compound:{Nested:1b}}
Kiểu dữ liệu: b (byte), s (short), i (int), l (long), f (float), d (double), B (byte array), I (int array), L (long array), s (string – cần dấu ngoặc kép "...").
7.2. Lệnh /data (Chỉ Dùng Được Trên Singleplayer/Localhost, Không Dùng Trên Server Dedicated Cho Player)
/data get block <pos> <path>: Xem NBT của Block Entity (Chest, Furnace, Command Block…)./data get entity @s <path>: Xem NBT của entity (Player, Mob, Item entity)./data merge block <pos> {nbt}: Gộp NBT mới vào block./data merge entity @s {nbt}: Gộp NBT vào entity.
7.3. Data Components (Phiên Bản 1.20.5 / 1.21+ – “The Flattening 2.0”)
Mojang đang chuyển từ NBT thô sang Data Components (component-based) cho item. Cú pháp mới trong lệnh /give, /item replace, /loot:
/give @p minecraft:diamond_swordminecraft:damage=100, minecraft:enchantments={levels:{"minecraft:sharpness":5}}, minecraft:custom_name='{"text":"Excalibur","color":"gold"}'
- Dùng ngoặc vuông
thay vì tag{tag:{...}}cũ. - Các component chuẩn:
minecraft:damage,minecraft:enchantments,minecraft:attribute_modifiers,minecraft:unbreakable,minecraft:custom_name,minecraft:lore,minecraft:hide_tooltip,minecraft:max_stack_size,minecraft:food,minecraft:tool…
Lưu ý: NBT cũ (
{tag:{Enchantments:...}}) vẫn hoạt động (backward compatibility) nhưng sẽ bị deprecated. Hãy học Data Components ngay bây giờ.
8. Scoreboard: Bộ Nhớ & Biến Toàn Cục

Có thể bạn quan tâm: Hướng Dẫn Chi Tiết Cách Làm Đồ Trong Minecraft: Từ Cơ Bản Đến Nâng Cao Cho Người Mới Bắt Đầu
Command Block không có “biến” (variable) như lập trình thông thường. Scoreboard chính là hệ thống biến.
8.1. Các Lệnh Cốt Lõi
/scoreboard objectives add <name> <criteria> displayname- Criteria quan trọng:
dummy(tăng/giảm thủ công),trigger(cho phép non-op sửa via/trigger),minecraft.custom:minecraft.<action>(statistics: walk, jump, kill, use_item, break_block…).
- Criteria quan trọng:
/scoreboard players set/get/add/remove/operation <targets> <objective> <value>/scoreboard players enable <targets> <trigger_objective>: Cho phép player dùng/trigger.
8.2. Ứng Dụng Thực Tế: Hệ Thống “Trigger” Cho Non-OP
Trên Server, player không OP không thể dùng Command Block. Giải pháp:
1. Tạo objective: /scoreboard objectives add settings trigger
2. Command Block Repeat/Always Active: /scoreboard players enable @a settings
3. Player gõ: /trigger settings set 1 -> Scoreboard settings của player đó thành 1.
4. Command Block Chain detect score=1 -> Thực thi logic (mở menu, bật/tắt setting) -> Reset score về 0.
8.3. “Fake Player” – Biến Toàn Cục Không Thuộc Ai
Sử dụng tên giả bắt đầu bằng # (ví dụ #global_timer, #config_difficulty).
Không phải entity thật -> Không bị @a, @e select.
Dùng lưu config map, timer, state machine.
Ví dụ: /scoreboard players add #global_timer time 1 (trong tick.mcfunction).
9. Tối Ưu Hiệu Năng (Performance/Lag Reduction) Trên PC
Viết lệnh chạy được khác với viết lệnh chạy tốt. Trên Java Edition, lag chủ yếu do Tick Loop (Main Thread) bị chậm.
9.1. Nguyên Tắc Vàng: Giảm Thiểu @e Và @a Không Cần Thiết
- Sai:
/execute as @e run ...(Chạy cho 1000 entity -> lag spike). - Đúng:
/execute as @etag=my_mob, distance=..50 run ...(Lọc tag + distance trước). - Tốt nhất: Dùng Predicate hoặc Tag để gom nhóm entity cần xử lý, tránh scan toàn bộ world.
9.2. Sử Dụng /execute if/unless Để “Short-Circuit” Sớm
# Sai: Chạy kill cho tất cả zombie, rồi mới check distance
execute as @etype=zombie run execute if entity @sdistance=..20 run kill @s # Đúng: Check distance trước (cheap), mới kill (expensive)
execute as @etype=zombie, distance=..20 run kill @s
Hoặc phức tạp hơn:
execute as @etag=boss at @s if block ~ ~-1 ~ minecraft:bedrock unless entity @etype=player,distance=..10 run say "Boss an toàn"
Game engine dừng ngay tại if/unless false, không chạy run.
9.3. Tránh Lặp Lệnh Trong Command Block GUI (Repeat + Always Active)
Nếu logic phức tạp, hãy chuyển sang Function + Tag tick.json.
Function được compile/parse 1 lần.
Command Block parse string mỗi tick (hoặc cache nhưng vẫn heavy hơn function).
9.4. Giới Hạn maxCommandChainLength & maxCommandForkCount

Có thể bạn quan tâm: Lâu Đài Địa Ngục Minecraft: Hướng Dẫn Toàn Diện Từ A-z Về Nether Fortress
- Mặc định: Chain length = 65536, Fork count = 65536.
- Nếu dùng đệ quy sâu (recursive function) hoặc
/execute storephức tạp, có thể hit limit. Có thể chỉnh trongserver.propertiesnhưng tốt nhất là tối ưu thuật toán.
9.5. Sử Dụng schedule Thay Vì Redstone Clock/Repeat Loop
- Redstone block update gây Block Update Lag (lighting recalc, neighbor update).
/schedule function name 20tchính xác 1 giây, zero lag Redstone.
10. Debug & Troubleshooting: “Tại Sao Lệnh Của Tôi Không Chạy?”
10.1. Dùng /say Hoặc /tellraw Debug
Chèn vào giữa chuỗi lệnh để biết code chạy đến đâu:
execute if score @s matches 1.. run say "DEBUG: Passed score check"
10.2. Kiểm Tra “Previous Output” Trong GUI
Nhấn nút O (góc dưới phải GUI) để bật hiển thị output trên màn hình. Xem dòng chữ đỏ (Error) hoặc xanh (Success).
10.3. Log File (latest.log)
Trên PC: %appdata%\.minecraft\logs\latest.log.
Tìm từ khóa Server thread/ERROR hoặc Command Block. File log ghi chi tiết lỗi parse NBT, syntax error mà GUI không hiển thị hết.
10.4. Lỗi Thường Gặp Trên Java Edition
- Sai cú pháp NBT/JSON: Thiếu dấu ngoặc, sai kiểu dữ liệu (ví dụ:
Count:1bthay vìCount:1cho byte). - Selector không khớp:
@etype=Zombie(sai, phảiminecraft:zombiehoặczombietùy version, tốt nhất dùng namespaceminecraft:zombie). - Execution Context sai: Dùng
@snhưng chưaas @e...hoặcat @ssai chỗ -> teleport sai chỗ, kill nhầm entity. - Dimension mismatch: Chạy lệnh ở Overworld nhưng target entity ở Nether -> Selector
@ekhông tìm thấy (trừ khi dùngin minecraft:the_nether). - Tick Order: Command Block chạy theo thứ tự: Chunk load -> Entities tick -> Block Entities tick (Command Block) -> Redstone. Nếu bạn summon entity và ngay lập tức
@eselect nó trong cùng 1 tick nhưng khác chunk/order, có thể không tìm thấy. Giải pháp: Dùng function tagtick(chạy sau entity tick) hoặcschedule 1t.
11. Ví Dụ Dự Án Thực Tế: Tạo Hệ Thống “Custom Crafting” (Bàn Chế Tác Tùy Chỉnh)
Đây là bài tập tổng hợp áp dụng hầu hết kiến thức trên.
Yêu cầu: Player thả 4 Iron Ingot + 1 Redstone xuống đất -> Tự biến thành 1 “Magnetic Core” (Custom Item).
Bước 1: Setup Datapack & Scoreboard
load.mcfunction:
scoreboard objectives add craft_check dummy
# Tag item entity để dễ quản lý
execute as @etype=item,nbt={Item:{id:"minecraft:iron_ingot"}} run tag @s add craft_iron
execute as @etype=item,nbt={Item:{id:"minecraft:redstone"}} run tag @s add craft_redstone
Bước 2: Logic Detect (tick.mcfunction)
# 1. Tìm vị trí có đủ nguyên liệu (gần nhau <= 1 block)
execute as @etag=craft_iron,limit=1 at @s if entity @etag=craft_redstone,distance=..1,limit=1 if entity @etag=craft_iron,distance=..1,limit=3 run function mypack:craft_success # 2. Hàm craft_success.mcfunction
# Kill nguyên liệu
kill @etag=craft_iron,distance=..1,limit=4
kill @etag=craft_redstone,distance=..1,limit=1
# Summon item kết quả tại vị trí trung bình (hoặc vị trí iron đầu tiên)
summon item ~ ~1 ~ {Item:{id:"minecraft:iron_ingot",Count:1b,components:{"minecraft:custom_name":'{"text":"Magnetic Core","color":"aqua","italic":false}',"minecraft:lore":'"",{"text":"Core từ tính","color":"gray"}',"minecraft:custom_data":{magnetic:1b}}}}
# Particle effect
particle minecraft:electric_spark ~ ~1 ~ 0.5 0.5 0.5 0.1 10
playsound minecraft:entity.experience_orb.pickup master @a ~ ~ ~ 1 1
Bước 3: Sử Dụng Item (Ví dụ: Click chuột phải hút item)

tick.mcfunction (thêm vào):
# Detect player dùng item có custom_data magnetic:1b
execute as @anbt={SelectedItem:{components:{"minecraft:custom_data":{magnetic:1b}}}} at @s anchored eyes positioned ^ ^ ^3 run function mypack:magnetic_pull
magnetic_pull.mcfunction:
# Hút item xung quanh bán kính 5 block
execute as @etype=item,distance=..5 at @s run tp @s ^ ^ ^-1
# Hoặc dùng motion vector phức tạp hơn để mượt mà
12. Các Lệnh “Quality of Life” Hay Dùng Cho Admin/Map Maker PC
| Lệnh | Công Dụng |
|---|---|
/gamerule sendCommandFeedback false |
Tắt spam log “Command executed…” trong chat cho OP. |
/gamerule logAdminCommands false |
Tắt log command trong file latest.log (giảm lag I/O disk). |
/gamerule maxCommandChainLength 2147483647 |
Tăng giới hạn chuỗi lệnh (cẩn thận stack overflow). |
/gamerule commandBlockOutput false |
Tắt output của Command Block ra chat (nếu dùng Always Active). |
/effect give @a minecraft:saturation 99999 255 true |
Giữ no mãi mãi (true = hide particles). |
/attribute @a minecraft:generic.max_health base set 40 |
Set max health 20 tim (40 nửa tim). |
/team add Admins {"text":"Admin","color":"red"} + /team join Admins @p |
Tạo team hiển thị prefix tên. |
/title @a title {"text":"Chào Mừng","color":"gold"} + /title @a subtitle {"text":"Đến Với Server"} |
Title/Subtitle màn hình. |
/bossbar add main {"text":"Boss HP","color":"red"} + /bossbar set main players @a |
Tạo Bossbar toàn server. |
13. Bảo Mật & Anti-Cheat Với Command Block (Server Side)
Trên PC Server, Command Block (hoặc Function) là lớp phòng thủ đầu tiên trước hack client.
13.1. Anti-Fly/NoFall (Basic)
# Trong tick.mcfunction
execute as @atag=!creative at @s if block ~ ~-0.1 ~ air if block ~ ~-1 ~ air unless entity @snbt={FallDistance:0.0f} run tp @s ~ ~ ~ # Reset pos hoặc kick
# Cách tốt hơn: Dùng predicate check motion/position delta.
13.2. Anti-Xray (Engine Mode 2 – Paper/Spigot)
Command Block không thể anti-xray hiệu quả. Cần Paper/Spigot/Purpur config anti-xray: engine-mode: 2. Command Block chỉ hỗ trợ log/giam sat.
13.3. Log Hành Động Đáng Ngờ
# Log khi player mở container bất thường (Enderchest far away?)
execute as @a store result score @s opened_container run clear @s minecraft:air 0
# Hoặc dùng advancement trigger minecraft:opened_inventory -> log qua function reward.
13.4. Giới Hạn Entity Cramming / Lag Machine
# Kill entity quá đông trong 1 block (trừ player, armor_stand, item_frame)
execute as @etype=#minecraft:impact_projectiles,limit=100,sort=nearest at @s if entity @etype=#minecraft:impact_projectiles,distance=..0.5,limit=50 run kill @etype=#minecraft:impact_projectiles,distance=..0.5,limit=49
14. Tài Nguyên Học Tập & Cộng Đồng Minecraft PC (Java Edition) Quốc Tế
Để đi sâu hơn, bạn bắt buộc phải đọc tài liệu tiếng Anh vì cộng đồng Việt Nam còn hạn chế về technical command advance.
- Minecraft Wiki (Official):
minecraft.wiki-> Search “Commands”, “Command Block”, “Data components”, “Execute”. Nguồn chính xác nhất. - Minecraft Commands Discord: Server Discord lớn nhất thế giới về command/technical. Kênh
#java-edition,#datapacks,#help. Có bot tự check syntax. - Misode’s Tools (Website):
misode.github.io-> Command Generator, Datapack Builder, Predicate Generator, Loot Table Generator. Công cụ không thể thiếu cho PC. - SliceLime / Gnembon / SciCraft (YouTube): Các kênh “Technical Minecraft” hàng đầu. Xem series “Command Block Tutorial” hoặc “Datapack Tutorial”.
- Markus Persson (Notch) / Dinnerbone / Slicedlime Tweets: Theo dõi các dev Mojang để nắm cập nhật snapshot (1.21, 1.22…).
15. Kết Luận: Con Đường Từ “Người Dùng Lệnh” Sang “Kỹ Sư Redstone/Command”
Lệnh Command Block trên Minecraft PC (Java Edition) không chỉ là gõ /give @p diamond. Đó là một ngôn ngữ lập trình khai báo (declarative) chạy trên game engine tick-based.
Để thành thạo, bạn cần trải qua 3 giai đoạn:
1. Syntax Mastery: Học thuộc cú pháp /execute, Selector, NBT/Data Components, Scoreboard.
2. Architecture Thinking: Chia nhỏ bài toán thành: Input (Detect) -> Process (Logic/Scoreboard) -> Output (Effect/Item/Entity). Sử dụng Datapack/Function thay vì GUI.
3. Optimization & Debug: Hiểu game loop, tick order, entity processing order để viết code không lag, dùng tool debug (Misode, /debug report, log file).
Hãy bắt đầu ngay hôm nay bằng việc tạo một Datapack đơn giản: Tự động nấu đồ ăn khi thả vào lửa (Campfire/Furnace) mà không cần fuel. Đó là bài tập “Hello World” tốt nhất để chuyển từ lý thuyết sang thực hành.
Chúc bạn xây dựng được những hệ thống “Vanilla Mod” tuyệt vời trên Minecraft Java Edition!
