Skip to Content
Minecraft模組設計|使用Java建立酷炫好玩的模組
book

Minecraft模組設計|使用Java建立酷炫好玩的模組

by Arun Gupta, Aditya Gupta
January 2016
Intermediate to advanced
200 pages
3h 22m
Chinese
GoTop Information, Inc.
Content preview from Minecraft模組設計|使用Java建立酷炫好玩的模組
打方塊訊息
|
25
在事件處理器內加入方法
同樣的,這只是通用的 Java 檔。你需要在其中加入你想要執行的程式。在我們的範例
中,它將會在玩家打破方塊時,傳送一個聊天訊息。首先要做的便是加入一個打破方塊
事件要執行的方法。如範例 2-4 所示,將其加在第一個括號後面。
要確認有使用 Ctrl-Shift-O Cmd-Shift-O 導入所有相關的類別。
範例
2-4 
打破方塊訊息事件處理器方法
@SubscribeEvent
public void sendMessage(BreakEvent event)
{
}
注意到方法上面的
@SubscribeEvent
註釋,這個註釋是 Forge 內建的,用來告訴它底下這
個方法是個事件處理器方法,並且在事件發生時來執行它。
BreakEvent event
這個參數
是告訴 Forge,這個方法在遇到
BreakEvent
,也就是打破方塊事件發生時,將會執行。
這個參數名稱是叫做
event
,可以在方法中被使用。
現在你必須在方法內加入一些程式,讓它在玩家打破方塊時可以傳遞訊息。程式如範例
2-5 所示,需要寫在
{
}
間來定義 sendMessage 方法的內容。同樣的,在程式加入之
後,要確認導入所有所需的類別。
範例
2-5 
打破方塊訊息方法的程式
event
.getPlayer()
.addChatComponentMessage(
new ChatComponentText(
EnumChatFormatting.GOLD +
"You broke ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

成為卓越程式設計師的38項必修法則

成為卓越程式設計師的38項必修法則

Pete Goodliffe
精實企業|高績效組織如何達成創新規模化

精實企業|高績效組織如何達成創新規模化

Jez Humble, Joanne Molesky, Barry O'Reilly
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone

Publisher Resources

ISBN: 9789863478751