Compare commits

...

4 Commits

Author SHA1 Message Date
Gerber Lóránt Viktor 4e502a8fed Add missing license headers
The license headers were missing. Oops.
2024-03-19 17:47:46 +01:00
Gerber Lóránt Viktor 1d98922bb2 Tidy BlockEntity mappings
This commit introduces a small functional interface
to make the type less ungodly. Also silences the warning
about referencing subclasses in the superclass, it is
fine in this case, we're just storing a reference to
the constructor.
2024-03-19 17:28:17 +01:00
Gerber Lóránt Viktor fca5da42f4 Change mapping type of BlockEntity lookups
This commit changes the type stored for BlockEntity
mappings from a class of the type associated with the
ID to a method reference to its constructor.
2024-03-19 17:22:11 +01:00
Gerber Lóránt Viktor 574bc6f9e8 Fix the coordinate-packing for block entity-loading
This commit fixes the incorrect shifting of bits when
packing the chunk-local coordinates of a block entity
into a 64-bit long for lookups.
2024-03-19 17:09:39 +01:00
7 changed files with 119 additions and 21 deletions

View File

@ -1,3 +1,27 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.world.block.entity;
import java.util.ArrayList;
@ -7,7 +31,7 @@ import java.util.Map;
public class BannerBlockEntity extends BlockEntity {
private final List<Pattern> patterns = new ArrayList<>();
private BannerBlockEntity(Map<String, Object> data) {
protected BannerBlockEntity(Map<String, Object> data) {
super(data);
@SuppressWarnings("unchecked")

View File

@ -1,3 +1,27 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.world.block.entity;
import com.google.gson.reflect.TypeToken;
@ -13,15 +37,23 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collectors;
@NBTDeserializer(BlockEntity.BlockEntityDeserializer.class)
public class BlockEntity {
private static final BlueNBT BLUENBT = new BlueNBT();
private static final Map<String, Class<? extends BlockEntity>> ID_MAPPING = Map.of(
"minecraft:sign", SignBlockEntity.class,
"minecraft:skull", SkullBlockEntity.class,
"minecraft:banner", BannerBlockEntity.class
@FunctionalInterface
private interface BlockEntityInitializer {
BlockEntity create(Map<String, Object> data);
}
@SuppressWarnings("StaticInitializerReferencesSubClass")
private static final Map<String, BlockEntityInitializer> ID_MAPPING = Map.of(
"minecraft:sign", SignBlockEntity::new,
"minecraft:skull", SkullBlockEntity::new,
"minecraft:banner", BannerBlockEntity::new
);
protected final String id;
@ -91,16 +123,10 @@ public class BlockEntity {
return null;
}
Class<? extends BlockEntity> dataClass = ID_MAPPING.getOrDefault(id, BlockEntity.class);
BlockEntityInitializer instance = ID_MAPPING.getOrDefault(id, BlockEntity::new);
try {
Constructor<? extends BlockEntity> constructor = dataClass.getDeclaredConstructor(Map.class);
if (constructor.trySetAccessible()) {
return constructor.newInstance(data);
}
} catch (NoSuchMethodException e) {
Logger.global.logError(
String.format("No constructor in %s that takes a Map!", dataClass.getCanonicalName()), e);
return instance.create(data);
} catch (Exception e) {
Logger.global.logError("Failed to instantiate BlockEntity instance!", e);
}

View File

@ -1,3 +1,27 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.world.block.entity;
import java.util.List;
@ -8,7 +32,7 @@ public class SignBlockEntity extends BlockEntity {
private final TextData backText;
@SuppressWarnings("unchecked")
private SignBlockEntity(Map<String, Object> data) {
protected SignBlockEntity(Map<String, Object> data) {
super(data);
// Versions before 1.20 used a different format

View File

@ -1,3 +1,27 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.core.world.block.entity;
import org.jetbrains.annotations.Nullable;
@ -12,7 +36,7 @@ public class SkullBlockEntity extends BlockEntity {
private final @Nullable String extraType;
private final @Nullable SkullOwner skullOwner;
private SkullBlockEntity(Map<String, Object> data) {
protected SkullBlockEntity(Map<String, Object> data) {
super(data);
this.noteBlockSound = (String) data.get("note_block_sound");

View File

@ -121,7 +121,7 @@ public class Chunk_1_13 extends MCAChunk {
}
this.blockEntities = level.blockEntities.stream().collect(Collectors.toMap(
it -> (long) it.getY() << 40 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
it -> (long) it.getY() << 8 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
));
}
@ -207,7 +207,7 @@ public class Chunk_1_13 extends MCAChunk {
@Override
public @Nullable BlockEntity getBlockEntity(int x, int y, int z) {
return blockEntities.get((long) y << 40 | (x & 0xF) << 4 | z & 0xF);
return blockEntities.get((long) y << 8 | (x & 0xF) << 4 | z & 0xF);
}
private @Nullable Section getSection(int y) {

View File

@ -120,7 +120,7 @@ public class Chunk_1_16 extends MCAChunk {
}
this.blockEntities = level.blockEntities.stream().collect(Collectors.toMap(
it -> (long) it.getY() << 40 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
it -> (long) it.getY() << 8 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
));
}
@ -203,7 +203,7 @@ public class Chunk_1_16 extends MCAChunk {
@Override
public @Nullable BlockEntity getBlockEntity(int x, int y, int z) {
return blockEntities.get((long) y << 40 | (x & 0xF) << 4 | z & 0xF);
return blockEntities.get((long) y << 8 | (x & 0xF) << 4 | z & 0xF);
}
private @Nullable Section getSection(int y) {

View File

@ -117,7 +117,7 @@ public class Chunk_1_18 extends MCAChunk {
}
this.blockEntities = data.blockEntities.stream().collect(Collectors.toMap(
it -> (long) it.getY() << 40 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
it -> (long) it.getY() << 8 | (it.getX() & 0xF) << 4 | it.getZ() & 0xF, it -> it
));
}
@ -195,7 +195,7 @@ public class Chunk_1_18 extends MCAChunk {
@Override
public @Nullable BlockEntity getBlockEntity(int x, int y, int z) {
return blockEntities.get((long) y << 40 | (x & 0xF) << 4 | z & 0xF);
return blockEntities.get((long) y << 8 | (x & 0xF) << 4 | z & 0xF);
}
private @Nullable Section getSection(int y) {