Skip to content

Commit

Permalink
feat: Новогоднее событие, ожидаем обновления сервера
Browse files Browse the repository at this point in the history
  • Loading branch information
MaXFeeD committed Dec 22, 2023
1 parent 0c04a63 commit 4b3c6d7
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/reider745/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public static void main(String[] args) throws Throwable {
loader.registerHooksInitializationForClass(BlocksHooks.class);
loader.registerHooksInitializationForClass(LevelHooks.class);
loader.registerHooksInitializationForClass(ItemUtils.class);
loader.registerHooksInitializationForClass(AndroidSdkHooks.class);
loader.registerHooksInitializationForClass(AndroidHooks.class);
loader.registerHooksInitializationForClass(SnowfallEverywhere.class);

loader.init();
loader.run("cn.nukkit.Nukkit", args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Locale;

@Hooks
public class AndroidSdkHooks implements HookClass {
public class AndroidHooks implements HookClass {
@FieldPatched(className = "android.os.Build.VERSION")
public static final String INCREMENTAL = "1661782216865";
@FieldPatched(className = "android.os.Build.VERSION")
Expand Down Expand Up @@ -276,6 +276,6 @@ public static int argb(float alpha, float red, float green, float blue) {
sColorNameMap.put("teal", 0xFF008080);
}

private AndroidSdkHooks() {
private AndroidHooks() {
}
}
108 changes: 108 additions & 0 deletions src/main/java/com/reider745/hooks/SnowfallEverywhere.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.reider745.hooks;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

import com.reider745.api.hooks.HookClass;
import com.reider745.api.hooks.annotation.Hooks;
import com.reider745.api.hooks.annotation.Inject;

import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntitySpawnable;
import cn.nukkit.level.DimensionData;
import cn.nukkit.level.biome.Biome;
import cn.nukkit.level.format.ChunkSection;
import cn.nukkit.level.format.generic.BaseChunk;
import cn.nukkit.level.format.generic.serializer.NetworkChunkSerializer;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.utils.BinaryStream;
import cn.nukkit.utils.ThreadCache;

import java.nio.ByteOrder;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

/**
* Special christmas adaptation, do not use anywhere else.
*/
@Hooks
public class SnowfallEverywhere implements HookClass {

@Inject(className = "cn.nukkit.level.format.generic.serializer.NetworkChunkSerializer")
public static void serialize(IntSet protocols, BaseChunk chunk,
Consumer<NetworkChunkSerializer.NetworkChunkSerializerCallback> callback, DimensionData dimensionData) {
for (int protocolId : protocols) {
byte[] blockEntities;
if (chunk.getBlockEntities().isEmpty()) {
blockEntities = new byte[0];
} else {
blockEntities = serializeEntities(chunk, protocolId);
}

int subChunkCount = 0;
ChunkSection[] sections = chunk.getSections();
for (int i = sections.length - 1; i >= 0; i--) {
if (!sections[i].isEmpty()) {
subChunkCount = i + 1;
break;
}
}

BinaryStream stream = ThreadCache.binaryStream.get().reset();
for (int i = 0; i < subChunkCount; i++) {
sections[i].writeTo(protocolId, stream);
}

stream.put(toSnowfallBiomeIdArray(chunk.getBiomeIdArray()));
// Border blocks
stream.putByte((byte) 0);
stream.put(blockEntities);

callback.accept(
new NetworkChunkSerializer.NetworkChunkSerializerCallback(protocolId, stream, subChunkCount));
}
}

private static byte[] toSnowfallBiomeIdArray(byte[] biomes) {
for (int i = 0, l = biomes.length; i < l; i++) {
Biome biome = Biome.getBiome(biomes[i] & 0xFF);
if (biome.isFreezing() || !biome.canRain()) {
continue;
}
biomes[i] = (byte) switch (biomes[i] & 0xFF) {
case 0, 24 -> 10; // OCEAN, DEEP_OCEAN -> FROZEN_OCEAN
case 7 -> 11; // RIVER -> FROZEN_RIVER
case 14, 15, 16, 25 -> 26; // MUSHROOM_ISLAND, MUSHROOM_ISLAND_SHORE, BEACH, STONE_BEACH -> COLD_BEACH
// PLAINS, FOREST, SWAMP, JUNGLE, BIRCH_FOREST, ROOFED_FOREST, SUNFLOWER_PLAINS,
// FLOWER_FOREST, SWAMPLAND_M, JUNGLE_M, JUNGLE_EDGE_M, BIRCH_FOREST_M,
// BIRCH_FOREST_HILLS_M, ROOFED_FOREST_M, EXTREME_HILLS_PLUS_M -> ICE_PLAINS
case 1, 4, 6, 21, 27, 29, 34, 131, 132, 134, 149, 151, 155, 156, 157, 162 -> 12;
// EXTREME_HILLS, FOREST_HILLS, EXTREME_HILLS_EDGE, JUNGLE_HILLS, JUNGLE_EDGE,
// BIRCH_FOREST_HILLS, EXTREME_HILLS_PLUS, EXTREME_HILLS_M -> ICE_PLAINS_SPIKES
case 3, 18, 20, 22, 23, 28, 129 -> 140;
case 5, 32, 160 -> 30; // TAIGA, MEGA_TAIGA, MEGA_SPRUCE_TAIGA -> COLD_TAIGA
case 133 -> 158; // TAIGA_M -> COLD_TAIGA_M
case 19, 33 -> 32; // TAIGA_HILLS, MEGA_TAIGA_HILLS -> COLD_TAIGA_HILLS
default -> 10; // FROZEN_OCEAN
};
}
return biomes;
}

private static byte[] serializeEntities(BaseChunk chunk, int protocol) {
List<CompoundTag> tagList = new ObjectArrayList<>();
for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
if (blockEntity instanceof BlockEntitySpawnable) {
tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound(protocol));
}
}

try {
return NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 4b3c6d7

Please sign in to comment.