zh_cn:tutorial:persistent_states
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
zh_cn:tutorial:persistent_states [2023/12/15 08:10] – created & translation started dreamuniverse | zh_cn:tutorial:persistent_states [2023/12/17 02:52] – Player Specific Persistent dreamuniverse | ||
---|---|---|---|
Line 103: | Line 103: | ||
那么,如果我们把上次游戏结束时的计数写入到了本地(如硬盘某处),我们就可以在游戏启动时读取并加载它,作为计数器的初始值。这样一来我们就可以继续统计下去。\\ | 那么,如果我们把上次游戏结束时的计数写入到了本地(如硬盘某处),我们就可以在游戏启动时读取并加载它,作为计数器的初始值。这样一来我们就可以继续统计下去。\\ | ||
但是,我们**必须**要做到这一点:数值要在游戏关闭时保存,在游戏启动时读取并加载。\\ | 但是,我们**必须**要做到这一点:数值要在游戏关闭时保存,在游戏启动时读取并加载。\\ | ||
- | 想要达到这一目的,方法千千万,不过我们在这里使用 Minecraft 提供给我们的方法:实现一个扩展了 ' | + | 想要达到这一目的,方法千千万,不过我们在这里使用 Minecraft 提供给我们的方法:实现一个继承了 '' |
====== 状态持久化 ====== | ====== 状态持久化 ====== | ||
Line 128: | Line 128: | ||
</ | </ | ||
- | 注:在扩展 | + | 注:在继承 |
- | * '' | + | * '' |
+ | | ||
- | Next add the following function to that same file: | + | 接下来,将这个函数添加到同一个文件中: |
<code java> | <code java> | ||
public class StateSaverAndLoader extends PersistentState { | public class StateSaverAndLoader extends PersistentState { | ||
- | // ... (Previously written code) | + | // ... (先前写好的代码部分) |
public static StateSaverAndLoader createFromNbt(NbtCompound tag) { | public static StateSaverAndLoader createFromNbt(NbtCompound tag) { | ||
Line 147: | Line 148: | ||
</ | </ | ||
- | This function does the opposite of '' | + | 这个函数的作用与 |
- | * Note: how we pull out the int we stored earlier with '' | + | * 注:与 |
- | Now we just need to add one more utility function which hooks everything up together. This function will take a '' | + | 现在我们再添加一个工具类函数,这个函数需要导入 |
+ | 当创建时,其调用我们刚刚写到的 | ||
<code java> | <code java> | ||
public class StateSaverAndLoader extends PersistentState { | public class StateSaverAndLoader extends PersistentState { | ||
- | // ... (Previously written code) | + | // ... (先前写好的代码部分) |
private static Type< | private static Type< | ||
- | StateSaverAndLoader:: | + | StateSaverAndLoader:: |
- | StateSaverAndLoader:: | + | StateSaverAndLoader:: |
- | null // Supposed to be an ' | + | null // 此处理论上应为 |
); | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
- | // (Note: arbitrary choice to use ' | + | // (注:如需在任意维度生效,请使用 |
PersistentStateManager persistentStateManager = server.getWorld(World.OVERWORLD).getPersistentStateManager(); | PersistentStateManager persistentStateManager = server.getWorld(World.OVERWORLD).getPersistentStateManager(); | ||
- | // The first time the following | + | // 当第一次调用了方法 |
- | // stores it inside the ' | + | // ' |
- | // ' | + | |
StateSaverAndLoader state = persistentStateManager.getOrCreate(type, | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, | ||
- | // If state is not marked | + | // 若状态未标记为脏(dirty),当 |
- | // Technically it's ' | + | // 从技术上讲,只有在事实上发生数据变更时才应当将状态标记为脏(dirty)。 |
- | // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | + | // 但大多数开发者和模组作者会对他们的数据未能保存而感到困惑,所以不妨直接使用 |
- | // Besides, it's literally just setting a bool to true, and the only time there' | + | // 另外,这只将对应的布尔值设定为 TRUE,代价是文件写入磁盘时模组的状态不会有任何改变。(这种情况非常少见) |
- | // there were no actual change to any of the mods state (INCREDIBLY RARE). | + | |
state.markDirty(); | state.markDirty(); | ||
Line 185: | Line 185: | ||
</ | </ | ||
- | Your '' | + | 现在,您的 |
<code java> | <code java> | ||
Line 211: | Line 211: | ||
private static Type< | private static Type< | ||
- | StateSaverAndLoader:: | + | StateSaverAndLoader:: |
- | StateSaverAndLoader:: | + | StateSaverAndLoader:: |
- | null // Supposed to be an ' | + | null // 此处理论上应为 |
); | ); | ||
public static StateSaverAndLoader getServerState(MinecraftServer server) { | public static StateSaverAndLoader getServerState(MinecraftServer server) { | ||
- | // (Note: arbitrary choice to use ' | + | // (注:如需在任意维度生效,请使用 |
PersistentStateManager persistentStateManager = server.getWorld(World.OVERWORLD).getPersistentStateManager(); | PersistentStateManager persistentStateManager = server.getWorld(World.OVERWORLD).getPersistentStateManager(); | ||
- | // The first time the following | + | // 当第一次调用了方法 |
- | // stores it inside the ' | + | // ' |
- | // ' | + | |
StateSaverAndLoader state = persistentStateManager.getOrCreate(type, | StateSaverAndLoader state = persistentStateManager.getOrCreate(type, | ||
- | // If state is not marked | + | // 若状态未标记为脏(dirty),当 |
- | // Technically it's ' | + | // 从技术上讲,只有在事实上发生数据变更时才应当将状态标记为脏(dirty)。 |
- | // of mod writers are just going to be confused when their data isn't being saved, and so it's best just to ' | + | // 但大多数开发者和模组作者会对他们的数据未能保存而感到困惑,所以不妨直接使用 |
- | // Besides, it's literally just setting a bool to true, and the only time there' | + | // 另外,这只将对应的布尔值设定为 TRUE,代价是文件写入磁盘时模组的状态不会有任何改变。(这种情况非常少见) |
- | // there were no actual change to any of the mods state (INCREDIBLY RARE). | + | |
state.markDirty(); | state.markDirty(); | ||
Line 237: | Line 235: | ||
</ | </ | ||
- | You'll also have to update your class which '' | + | 相应地,您也需要将实现了 |
<code java> | <code java> | ||
Line 253: | Line 251: | ||
public class ExampleMod implements ModInitializer { | public class ExampleMod implements ModInitializer { | ||
- | public static final String MOD_ID = "your_unique_mod_id_change_me_please"; | + | public static final String MOD_ID = "您的MOD_ID"; |
public static final Identifier DIRT_BROKEN = new Identifier(MOD_ID, | public static final Identifier DIRT_BROKEN = new Identifier(MOD_ID, | ||
Line 262: | Line 260: | ||
if (state.getBlock() == Blocks.GRASS_BLOCK || state.getBlock() == Blocks.DIRT) { | if (state.getBlock() == Blocks.GRASS_BLOCK || state.getBlock() == Blocks.DIRT) { | ||
StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(world.getServer()); | StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(world.getServer()); | ||
- | // Increment the amount of dirt blocks that have been broken | + | // 当泥土方块被挖掘时增加计数 |
serverState.totalDirtBlocksBroken += 1; | serverState.totalDirtBlocksBroken += 1; | ||
- | // Send a packet to the client | + | // 向客户端发送数据包 |
MinecraftServer server = world.getServer(); | MinecraftServer server = world.getServer(); | ||
Line 281: | Line 279: | ||
</ | </ | ||
- | If you run your game now, you should see the counter going up, but now, if you fully close Minecraft, and open it again, you should see the number keeps increasing from where it left off. | + | 如果您现在运行游戏,您会发现计数器正常递增,但现在即使您关闭游戏并重新启动,计数器也会从退出时的位置继续递增。 |
+ | |||
+ | 有一点您有可能会忽略,'' | ||
+ | 这对我们模组中的特定类型数据是很好的,表明它们能正常工作。但更多时候,我们期望数据是玩家限定(因人而异)的。 | ||
+ | 正如我们开篇所提,如果我们想要保留**任意玩家**的**特定方块**的挖掘数据,这时我们要怎么做? | ||
- | What you might or might not expect is that '' | ||
====== Player Specific Persistent State ====== | ====== Player Specific Persistent State ====== | ||
zh_cn/tutorial/persistent_states.txt · Last modified: 2023/12/23 02:39 by dreamuniverse