Skip to content

Commit

Permalink
Основа пользовательских зачарований
Browse files Browse the repository at this point in the history
  • Loading branch information
Reider745 committed Feb 11, 2024
1 parent 47e7b96 commit 2d55219
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.reider745.hooks.BiomesHooks;
import com.reider745.hooks.SnowfallEverywhere;
import com.reider745.hooks.bugfix.DimensionsFix;
import com.reider745.item.CustomEnchantMethods;
import com.reider745.item.CustomItem;

import com.reider745.item.ItemMethod;
Expand Down Expand Up @@ -309,6 +310,7 @@ public void preload(Server server) throws Exception {
}

public void afterload() {
CustomEnchantMethods.postInit();
Server.getInstance().getLogger().info("Registering Nukkit-MOT containment...");
PluginManager pluginManager = Server.getInstance().getPluginManager();
pluginManager.getPlugins().put(plugin.getDescription().getName(), plugin);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reider745/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static void main(String[] args) throws Throwable {
loader.registerHooksInitializationForClass(SnowfallEverywhere.class);
}

// loader.registerHooksInitializationForClass(BiomesHooks.class);
loader.registerHooksInitializationForClass(BiomesHooks.class);
loader.registerHooksInitializationForClass(GlobalBanList.class);

// bug fix
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/com/reider745/api/CallbackHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ public enum Type {
PRE_GENERATION_CHUNK_CUSTOM;
}

private static final HashMap<Type, ThreadRegion> types = new HashMap<>();
private static final HashMap<Type, ThreadCustomValue<Level>> types = new HashMap<>();

public interface ICallbackApply {
void apply();
}

private static class ThreadRegion extends Thread {
private Level region;
public static class ThreadCustomValue<T> extends Thread {
private T value;
private final ArrayList<ICallbackApply> applys = new ArrayList<>();

private final Level getRegion() {
if (region == null) {
System.out.println("isNull: true");
}
return region;
public final T getValue() {
return value;
}

public void add(ICallbackApply apply) {
Expand All @@ -61,7 +58,7 @@ public void run() {
}

public static void registerCallback(Type type) {
ThreadRegion threadRegion = new ThreadRegion();
ThreadCustomValue<Level> threadRegion = new ThreadCustomValue<>();
threadRegion.setName(type.name());
threadRegion.start();

Expand Down Expand Up @@ -130,8 +127,8 @@ public void run() {
}

public static void applyRegion(Type type, Level level, ICallbackApply apply) {
ThreadRegion threadRegion = types.get(type);
threadRegion.region = level;
ThreadCustomValue<Level> threadRegion = types.get(type);
threadRegion.value = level;
threadRegion.add(apply);
}

Expand All @@ -145,6 +142,19 @@ public static void apply(Event event, ICallbackApply apply, boolean isPrevented)
}
}

public static <T>ThreadCustomValue<T> applyCustomValue(String name, ICallbackApply apply, T def) {
ThreadCustomValue<T> thread = new ThreadCustomValue<>();
thread.value = def;
thread.add(apply);
thread.setName(name);
thread.start();

while (thread.isAlive()) {
java.lang.Thread.yield();
}
return thread;
}

public static void prevent() {
Thread thread = Thread.currentThread();
if (thread instanceof ThreadCallback threadCallback) {
Expand All @@ -160,10 +170,16 @@ public static boolean isPrevent() {
return false;
}

public static <T>void setValueForCurrent(T value){
Thread thread = Thread.currentThread();
if (thread instanceof ThreadCustomValue)
((ThreadCustomValue<T>) thread).value = value;
}

public static Level getForCurrentThread() {
Thread thread = Thread.currentThread();
if (thread instanceof ThreadRegion threadCallback) {
return threadCallback.getRegion();
if (thread instanceof ThreadCustomValue threadCallback) {
return (Level) threadCallback.getValue();
}
return null;
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ public void onCustomDimensionTransfer(EntityTeleportEvent event){

// TODO: onPathNavigationDone

// TODO: onAnimateBlockTick

// TODO: onBlockSpawnResources

// TODO: onBlockEventEntityInside
Expand All @@ -350,8 +348,5 @@ public void onCustomDimensionTransfer(EntityTeleportEvent event){

// TODO: onItemDispensed

// TODO: onEnchantPostAttack, onEnchantPostHurt, onEnchantGetDamageBonus,
// onEnchantGetProtectionBonus

// TODO: onWorkbenchCraft
}
4 changes: 2 additions & 2 deletions src/main/java/com/reider745/hooks/BiomesHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void rebuildField(CtClass ctClass, CtField field) {
}

public static void init() throws NoSuchFieldException {
//ReflectHelper.setField(Biome.class, "biomes", new Biome[MAX_ID]);
ReflectHelper.setField(Biome.class, "biomes", new Biome[MAX_ID]);
}

@Inject(className = "cn.nukkit.network.protocol.BiomeDefinitionListPacket", type = TypeHook.BEFORE_REPLACE)
Expand Down Expand Up @@ -73,7 +73,7 @@ public static Biome pickBiome(Normal self, int x, int z) {
public static void setBiomeMap(int x, int z, int id){
final Biome[][] biomes_override = threads_biomes_replace.get(Thread.currentThread().getId());
if(biomes_override != null){
biomes_override[Math.abs(x % 16)][Math.abs(z % 16)] = Biome.getBiome(id);
biomes_override[x % 16][z % 16] = Biome.getBiome(id);
}
}
}
182 changes: 182 additions & 0 deletions src/main/java/com/reider745/item/CustomEnchantMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.reider745.item;

import cn.nukkit.entity.Entity;
import cn.nukkit.entity.EntityHuman;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.item.*;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.item.enchantment.EnchantmentType;
import com.reider745.api.CallbackHelper;
import com.reider745.api.Client;
import com.reider745.api.pointers.PointersStorage;
import com.reider745.api.pointers.pointer_gen.PointerGenFastest;
import com.zhekasmirnov.innercore.api.NativeCallback;

// TODO: onEnchantPostAttack, onEnchantPostHurt,

public class CustomEnchantMethods {
private static final PointersStorage<Enchant> pointers = new PointersStorage<>("enchants", new PointerGenFastest(), false);
private static class Enchant extends Enchantment {
private int minLevel, maxLevel, frequency;
private final int[] types = new int[2];
private Item item_book = null;
private boolean isLootable, isDiscoverable, isMeleeDamage, isProtection, isTreasure_;

protected Enchant(int id, String name, Rarity rarity, EnchantmentType type) {
super(id, name, rarity, type);
Enchantment.register(this, false);
}

@Override
public int getMinLevel() {
return minLevel;
}

@Override
public int getMaxLevel() {
return maxLevel;
}

@Override
public boolean isTreasure() {
return isTreasure_;
}

public void postInit(){
item_book = Item.get(Item.ENCHANTED_BOOK);

item_book.addEnchantment(this);
}

public boolean canEnchantItem(int id, Item item) {
if (id == 16383 || id == -1) {
return true;
} else if (item instanceof ItemArmor) {
if (item.isArmor()) {
return true;
} else {
switch (id) {
case 1:
return item.isHelmet();
case 2:
return item.isChestplate();
case 3:
return item.isLeggings();
case 4:
return item.isBoots();
default:
return false;
}
}
} else {
if((item.isSword() || item.isAxe() || item instanceof ItemBow) && id == 16)
return true;
switch (id) {
case 16:
return item.isSword();
case 64:
return item.isHoe();
case 32:
return item instanceof ItemBow;
case 4096:
return item instanceof ItemFishingRod;
/* case WEARABLE:
return item instanceof ItemSkull;*/
/*case TRIDENT:
return item instanceof ItemTrident;*/
default:
return false;
}
}
}

@Override
public boolean canEnchant(Item item) {
for(int type : types)
if(type != 0 && !canEnchantItem(id, item))
return false;
return true;
}

@Override
public double getDamageBonus(Entity entity) {
if(!isMeleeDamage)
return 0;

Item item = entity instanceof EntityHuman human ? human.getInventory().getItemInHand() : Item.AIR_ITEM.clone();

return super.getDamageBonus(entity) + CallbackHelper.applyCustomValue("onEnchantGetDamageBonus", () ->
NativeCallback.onEnchantGetDamageBonus(this.id, item.getAttackDamage(), entity.getId()), 0f).getValue();
}

@Override
public float getProtectionFactor(EntityDamageEvent event) {
if(!isProtection)
return 0;

return super.getProtectionFactor(event) + CallbackHelper.applyCustomValue("onEnchantGetProtectionBonus", () ->
NativeCallback.onEnchantGetProtectionBonus(this.id, (int) event.getDamage() * 2, event.getCause().ordinal(), event.getEntity().getId()),
0f).getValue();
}
}

public static long constructNew(int id, String nameId) {
return pointers.addPointer(new Enchant(id, nameId, Enchantment.Rarity.COMMON, EnchantmentType.ALL));
}

@Client //Наверно клиент
public static void setDescription(long pointer, String description) {
}

public static void setFrequency(long pointer, int frequency) {
pointers.get(pointer).frequency = frequency;
}

public static void setIsLootable(long pointer, boolean isLootable) {
pointers.get(pointer).isLootable = isLootable;
}

public static void setIsDiscoverable(long pointer, boolean isDiscoverable) {
pointers.get(pointer).isDiscoverable = isDiscoverable;
}

public static void setIsTreasure(long pointer, boolean isTreasure) {
pointers.get(pointer).isTreasure_ = isTreasure;
}

public static void setIsMeleeDamageEnchant(long pointer, boolean value) {
pointers.get(pointer).isMeleeDamage = value;
}

public static void setIsProtectionEnchant(long pointer, boolean value) {
pointers.get(pointer).isProtection = value;
}

public static void setMasks(long pointer, int mask1, int mask2) {
Enchant enchant = pointers.get(pointer);

enchant.types[0] = mask1;
enchant.types[1] = mask2;
}

public static void setMinMaxLevel(long pointer, int minLevel, int maxLevel) {
Enchant enchant = pointers.get(pointer);
enchant.minLevel = minLevel;
enchant.maxLevel = maxLevel;
}

public static void setMinMaxCostPoly(long pointer, float aMin, float bMin, float cMin, float aMax, float bMax, float cMax) {
}

public static void passCurrentDamageBonus(float bonus) {
CallbackHelper.setValueForCurrent(bonus);
}

public static void passCurrentProtectionBonus(float bonus) {
CallbackHelper.setValueForCurrent(bonus);
}

public static void postInit(){
pointers.getPointers().values().forEach(v -> v.get().postInit());
}
}
Loading

0 comments on commit 2d55219

Please sign in to comment.