Skip to content

Commit

Permalink
Fixed: получение свойств блоков накита
Browse files Browse the repository at this point in the history
  • Loading branch information
Reider745 committed Jan 10, 2025
1 parent d3a9226 commit 8e793cd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ public void afterload() {
NativeFurnaceRegistry.init();
CommandsHelper.init();

ItemMethod.isPostLoad = true;

endLoad = true;

NativeCallback.onLevelCreated();
Expand Down
45 changes: 37 additions & 8 deletions src/main/java/com/reider745/block/BlockMethods.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package com.reider745.block;

import cn.nukkit.block.Block;
import com.reider745.InnerCoreServer;
import it.unimi.dsi.fastutil.Function;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

// TODO: не поддерживается переопределение свойств накита
public class BlockMethods {
private static final Int2ObjectOpenHashMap<Byte2ObjectOpenHashMap<Object>> id2typeMap = new Int2ObjectOpenHashMap<>();
private static final byte SOLID = 0;
private static final byte CAN_CONTAIN_LIQUID = 1;
private static final byte DESTROY_TIME = 2;
private static final byte TRANSLUCENCY = 3;
private static final byte EXPLOSION_RESISTANCE = 4;
private static final byte FRICTION = 5;
private static final byte LIGHT_LEVEL = 6;

public static final byte SOLID = 0;
public static final byte CAN_CONTAIN_LIQUID = 1;
public static final byte DESTROY_TIME = 2;
public static final byte TRANSLUCENCY = 3;
public static final byte EXPLOSION_RESISTANCE = 4;
public static final byte FRICTION = 5;
public static final byte LIGHT_LEVEL = 6;

private static Byte2ObjectOpenHashMap<Object> assureId2Type(int id) {
Byte2ObjectOpenHashMap<Object> type = id2typeMap.get(id);
if (type == null) {
type = new Byte2ObjectOpenHashMap<Object>();
type = new Byte2ObjectOpenHashMap<>();
id2typeMap.put(id, type);
}
return type;
Expand All @@ -28,6 +32,23 @@ private static Object getOrDefault(int id, byte property, Object defaultValue) {
return type != null ? type.getOrDefault(property, defaultValue) : defaultValue;
}

public static Byte2ObjectOpenHashMap<Object> getProperties(int id) {
return id2typeMap.computeIfAbsent(id, id_ -> {
throw new RuntimeException("Not found");
});
}

private interface IApply<T> {
T apply(Block block);
}

private static <T>T validateThen(int id, IApply<T> function, T fallback) {
final Block block = Block.get(id);
if(block != null)
return function.apply(block);
return fallback;
}

public static int getMaterial(int id) {
return 0;
}
Expand All @@ -45,14 +66,20 @@ public static boolean canBeExtraBlock(int id) {
}

public static float getDestroyTime(int id) {
if(InnerCoreServer.canEndLoadMods())
return validateThen(id, block -> (float) block.getHardness(),1f);
return (float) getOrDefault(id, DESTROY_TIME, 1f);
}

public static float getExplosionResistance(int id) {
if(InnerCoreServer.canEndLoadMods())
return validateThen(id, block -> (float) block.getResistance(), 1f);
return (float) getOrDefault(id, EXPLOSION_RESISTANCE, 1f);
}

public static float getFriction(int id) {
if(InnerCoreServer.canEndLoadMods())
return validateThen(id, block -> (float) block.getFrictionFactor(), 0.6f);
return (float) getOrDefault(id, FRICTION, 0.6f);
}

Expand All @@ -61,6 +88,8 @@ public static float getTranslucency(int id) {
}

public static int getLightLevel(int id) {
if(InnerCoreServer.canEndLoadMods())
return validateThen(id, block -> block.getLightLevel(), Block.light[id]);
return (int) getOrDefault(id, LIGHT_LEVEL, Block.light[id]);
}

Expand Down
29 changes: 21 additions & 8 deletions src/main/java/com/reider745/block/CustomBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.zhekasmirnov.innercore.api.NativeItem;
import com.zhekasmirnov.innercore.api.unlimited.BlockShape;

import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -148,8 +149,9 @@ public static CustomManager registerBlock(String textId, int id, String name, in
private int id;
private String name;

private boolean TickingTile;
private boolean NeighbourChange;
private final boolean TickingTile;
private final boolean NeighbourChange;
private final Byte2ObjectOpenHashMap<Object> properties;

protected CustomBlock(int id, int meta) {
this(id, meta, getBlockManager(id));
Expand All @@ -168,6 +170,12 @@ protected CustomBlock(int id, int meta, CustomManager manager, String name) {

TickingTile = manager.get("TickingTile:" + meta, false);
NeighbourChange = manager.get("NeighbourChange", false);

properties = BlockMethods.getProperties(id);
}

public <T>T getOrDefault(byte property, T defaultValue) {
return (T) properties.getOrDefault(property, defaultValue);
}

public static int getIdForText(String block) {
Expand All @@ -186,32 +194,37 @@ public boolean canRandomTickBlocks() {

@Override
public boolean isSolid() {
return BlockMethods.isSolid(id);
return (boolean) getOrDefault(BlockMethods.SOLID, true);
}

@Override
public double getResistance() {
return BlockMethods.getExplosionResistance(id);
return (double) getOrDefault(BlockMethods.EXPLOSION_RESISTANCE, 1f);
}

@Override
public double getHardness() {
return (double) getOrDefault(BlockMethods.DESTROY_TIME, 1f);
}

@Override
public double getFrictionFactor() {
return BlockMethods.getFriction(id);
return (double) getOrDefault(BlockMethods.FRICTION, 0.6f);
}

@Override
public int getLightLevel() {
return BlockMethods.getLightLevel(id);
return (int) getOrDefault(BlockMethods.LIGHT_LEVEL, Block.light[id]);
}

@Override
public boolean isTransparent() {
return BlockMethods.getTranslucency(id) >= 1f;
return ((float) getOrDefault(BlockMethods.TRANSLUCENCY, 0f)) >= 1f;
}

@Override
public boolean canBeFlowedInto() {
return BlockMethods.canContainLiquid(id);
return (boolean) getOrDefault(BlockMethods.CAN_CONTAIN_LIQUID, false);
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/reider745/hooks/block/BlocksHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public static void init() {
// ReflectHelper.setField(Block.class, "usesFakeWater", new boolean[MAX_ID]);
}

@Inject
// TODO: не поддерживается переопределение свойств накита

/*@Inject
public static boolean isSolid(Block self) {
return BlockMethods.isSolid(self.getId());
}
Expand All @@ -37,5 +39,5 @@ public static double getFrictionFactor(Block self) {
@Inject
public static int getLightLevel(Block self) {
return BlockMethods.getLightLevel(self.getId());
}
}*/
}
7 changes: 3 additions & 4 deletions src/main/java/com/reider745/item/ItemMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import com.reider745.InnerCoreServer;
import com.reider745.api.CustomManager;
import com.reider745.block.CustomBlock;
import com.reider745.hooks.item.ItemUtils;
Expand All @@ -14,8 +15,6 @@
import java.util.HashMap;

public class ItemMethod {
public static boolean isPostLoad = false;

public static class PropertiesNames {
public static final String ID = "id";
public static final String MAX_DAMAGE = "max_damage";
Expand Down Expand Up @@ -48,7 +47,7 @@ private static CustomManager getCustomManager(int id) {
}

public static int getMaxDamageForId(int id, int data) {
if (isPostLoad)
if (InnerCoreServer.canEndLoadMods())
return ItemUtils.get(id, data).getMaxDurability();
CustomManager manager = getCustomManager(id);
if (manager != null)
Expand Down Expand Up @@ -78,7 +77,7 @@ public static int getCreativeCategory(int id) {
}

public static int getMaxStackForId(int id, int data) {
if (isPostLoad)
if (InnerCoreServer.canEndLoadMods())
return ItemUtils.get(id, data).getMaxStackSize();
CustomManager manager = getCustomManager(id);
if (manager != null)
Expand Down

0 comments on commit 8e793cd

Please sign in to comment.