-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mechanism for retrieving BlockEntity data
This commit adds a mechanism for retrieving block entity data. Block entity data is required to support for example text on signs, banner patterns, or mods such as Domum Ornamentum.
- Loading branch information
Showing
7 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/BlockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package de.bluecolored.bluemap.core.world.block; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class BlockEntity { | ||
private static final List<String> COMMON_KEYS = List.of("id", "x", "y", "z", "keepPacked"); | ||
|
||
private final String id; | ||
private final int x, y, z; | ||
private final boolean keepPacked; | ||
private final Map<String, Object> data; | ||
|
||
public BlockEntity(Map<String, Object> rawData) { | ||
this.id = getString(rawData, "id", ""); | ||
this.x = getInt(rawData, "x", 0); | ||
this.y = getInt(rawData, "y", 0); | ||
this.z = getInt(rawData, "z", 0); | ||
this.keepPacked = getBoolean(rawData, "keepPacked", false); | ||
|
||
this.data = rawData.entrySet().stream() | ||
.filter(it -> !COMMON_KEYS.contains(it.getKey())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
public int getInt(Map<String, Object> tree, String key, int def) { | ||
Object obj = get(tree, key, String.valueOf(def)); | ||
|
||
try { | ||
return Integer.parseInt(String.valueOf(obj)); | ||
} catch (Exception e) { | ||
return def; | ||
} | ||
} | ||
|
||
public int getInt(String key, int def) { | ||
return getInt(data, key, def); | ||
} | ||
|
||
public boolean getBoolean(Map<String, Object> tree, String key, boolean def) { | ||
Object obj = get(tree, key, String.valueOf(def)); | ||
|
||
try { | ||
return Boolean.parseBoolean(String.valueOf(obj)); | ||
} catch (Exception e) { | ||
return def; | ||
} | ||
} | ||
|
||
public boolean getBoolean(String key, boolean def) { | ||
return getBoolean(data, key, def); | ||
} | ||
|
||
public String getString(Map<String, Object> tree, String key, String def) { | ||
Object obj = get(tree, key, String.valueOf(def)); | ||
|
||
try { | ||
return String.valueOf(obj); | ||
} catch (Exception e) { | ||
return def; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Map<String, Object> getSubtree(Map<String, Object> tree, String key) { | ||
Object obj = get(tree, key, Map.of()); | ||
|
||
if (!(obj instanceof Map)) { | ||
return Map.of(); | ||
} | ||
|
||
return (Map<String, Object>) obj; | ||
} | ||
|
||
public Map<String, Object> getSubtree(String key) { | ||
return getSubtree(data, key); | ||
} | ||
|
||
public Object get(Map<String, Object> tree, String key, Object def) { | ||
return tree.getOrDefault(key, def); | ||
} | ||
|
||
public Object get(String key, Object def) { | ||
return get(data, key, def); | ||
} | ||
|
||
public String getString(String key, String def) { | ||
return getString(data, key, def); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
public int getZ() { | ||
return z; | ||
} | ||
|
||
public boolean isKeepPacked() { | ||
return keepPacked; | ||
} | ||
|
||
public Map<String, Object> getData() { | ||
return data; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BlockEntity{" + | ||
"id='" + id + '\'' + | ||
", x=" + x + | ||
", y=" + y + | ||
", z=" + z + | ||
", keepPacked=" + keepPacked + | ||
", data=" + data + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
BlockEntity that = (BlockEntity) o; | ||
return x == that.x && y == that.y && z == that.z && keepPacked == that.keepPacked | ||
&& Objects.equals(id, that.id) && Objects.equals(data, that.data); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, x, y, z, keepPacked, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters