add positions to runes

This commit is contained in:
zongor 2024-11-17 00:19:35 -05:00
parent 6a94d4c45a
commit 85db38a9a8
8 changed files with 94 additions and 3 deletions

View File

@ -1,3 +1,3 @@
# futhark-fabric # futhark-fabric
A minecraft alpha era inspired mod adding old functionality, nostalgic items, and a magic system inspired by the Bukkit plugin Runecraft A minecraft magic mod inspired by the alpha era Bukkit plugin [Runecraft](https://dev.bukkit.org/projects/runecraft)

View File

@ -0,0 +1,13 @@
package com.futhark;
import net.minecraft.util.math.BlockPos;
public class RuneData {
public RuneType type;
public BlockPos pos;
@Override
public String toString() {
return String.format("%s (%d %d %d)", type.toString(), pos.getX(), pos.getY(), pos.getZ());
}
}

View File

@ -0,0 +1,39 @@
package com.futhark;
import java.util.HashMap;
import java.util.Map;
public enum RuneType {
UNKNOWN(0),
INCOMPLETE_TIER_0(1),
INCOMPLETE_TIER_1(2),
INCOMPLETE_TIER_2(3),
TELEPORT_START(4),
TELEPORT_END(5);
private final int value;
RuneType(int value) {
this.value = value;
}
public int asInt() {
return value;
}
private static final Map<Integer, RuneType> intToTypeMap = new HashMap<>();
static {
for (RuneType type : RuneType.values()) {
intToTypeMap.put(type.value, type);
}
}
public static RuneType fromInt(int value) {
RuneType type = intToTypeMap.get(Integer.valueOf(value));
if (type == null) {
throw new IllegalArgumentException("Unknown value: " + value);
}
return type;
}
}

View File

@ -2,23 +2,53 @@ package com.futhark;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.PersistentState; import net.minecraft.world.PersistentState;
import net.minecraft.world.PersistentStateManager; import net.minecraft.world.PersistentStateManager;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.HashMap;
public class StateSaverAndLoader extends PersistentState { public class StateSaverAndLoader extends PersistentState {
public Integer totalNumberOfRunes = 0; public Integer totalNumberOfRunes = 0;
public HashMap<BlockPos, RuneData> runes = new HashMap<>();
@Override @Override
public NbtCompound writeNbt(NbtCompound nbt) { public NbtCompound writeNbt(NbtCompound nbt) {
nbt.putInt("totalNumberOfRunes", totalNumberOfRunes); nbt.putInt("totalNumberOfRunes", totalNumberOfRunes);
NbtCompound runesNbt = new NbtCompound();
runes.forEach((pos, runeData) -> {
NbtCompound runeNbt = new NbtCompound();
runeNbt.putInt("type", runeData.type.asInt());
runesNbt.put(Long.toString(pos.asLong()), runeNbt);
});
nbt.put("runes", runesNbt);
return nbt; return nbt;
} }
public static StateSaverAndLoader createFromNbt(NbtCompound tag) { public static StateSaverAndLoader createFromNbt(NbtCompound tag) {
StateSaverAndLoader state = new StateSaverAndLoader(); StateSaverAndLoader state = new StateSaverAndLoader();
state.totalNumberOfRunes = tag.getInt("totalNumberOfRunes"); state.totalNumberOfRunes = tag.getInt("totalNumberOfRunes");
NbtCompound runesNbt = tag.getCompound("runes");
runesNbt.getKeys().forEach(key -> {
long longPos = Long.parseLong(key);
BlockPos pos = BlockPos.fromLong(longPos);
NbtCompound runeNbt = runesNbt.getCompound(key);
RuneData runeData = new RuneData();
runeData.type = RuneType.fromInt(runeNbt.getInt("type"));
runeData.pos = pos;
state.runes.put(pos, runeData);
});
return state; return state;
} }

View File

@ -26,8 +26,8 @@ public class RuneBlock extends Block {
MinecraftServer server = world.getServer(); MinecraftServer server = world.getServer();
if (server != null) { if (server != null) {
StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server); StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server);
player.sendMessage(Text.literal(String.format("Clicked rune at (%d,%d,%d) Total Runes in World: %d", pos.getX(), pos.getY(), pos.getZ(), serverState.totalNumberOfRunes))); player.sendMessage(Text.literal(String.format("Clicked rune %s Total Runes in World: %d", serverState.runes.get(pos), serverState.totalNumberOfRunes)));
world.playSound(player, pos, SoundEvents.BLOCK_PORTAL_TRIGGER, SoundCategory.BLOCKS, 1f, 1f); world.playSound(player, pos, SoundEvents.BLOCK_AMETHYST_BLOCK_CHIME, SoundCategory.BLOCKS, 1f, 1f);
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;

View File

@ -1,5 +1,7 @@
package com.futhark.item; package com.futhark.item;
import com.futhark.RuneData;
import com.futhark.RuneType;
import com.futhark.StateSaverAndLoader; import com.futhark.StateSaverAndLoader;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -10,6 +12,8 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.UUID;
import static com.futhark.block.FutharkBlocks.RUNE_BLOCK; import static com.futhark.block.FutharkBlocks.RUNE_BLOCK;
public class RunicChiselItem extends Item { public class RunicChiselItem extends Item {
@ -30,11 +34,16 @@ public class RunicChiselItem extends Item {
if (server != null) { if (server != null) {
StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server); StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server);
serverState.totalNumberOfRunes += 1; serverState.totalNumberOfRunes += 1;
RuneData runeData = new RuneData();
runeData.type = RuneType.UNKNOWN;
runeData.pos = pos;
serverState.runes.put(pos, runeData);
} }
} else if (state.isOf(RUNE_BLOCK)) { } else if (state.isOf(RUNE_BLOCK)) {
context.getWorld().setBlockState(pos, Blocks.STONE.getDefaultState()); context.getWorld().setBlockState(pos, Blocks.STONE.getDefaultState());
if (server != null) { if (server != null) {
StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server); StateSaverAndLoader serverState = StateSaverAndLoader.getServerState(server);
serverState.runes.remove(pos);
serverState.totalNumberOfRunes -= 1; serverState.totalNumberOfRunes -= 1;
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB