Skip to content

Commit

Permalink
Feat: Поддержка точных координат нажатия в ItemUse & Исправлено удале…
Browse files Browse the repository at this point in the history
…ние рецептов

- Добавлена проверка на blockSource при разрушении TileEntity
  • Loading branch information
Reider745 committed May 12, 2024
1 parent 8893dfa commit 0a0009a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
21 changes: 15 additions & 6 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@
import com.reider745.Main;
import com.reider745.world.BiomesMethods;
import com.reider745.world.FakeDimensions;
import com.zhekasmirnov.apparatus.mcpe.NativeWorkbench;
import oshi.hardware.NetworkIF;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import com.reider745.api.CallbackHelper;
import com.reider745.api.CustomManager;
Expand Down Expand Up @@ -93,6 +96,8 @@ protected void updateBlocks(Level level, double x, double y, double z, boolean s
level.sendBlocks(players, new Vector3[]{new Vector3(x, y, z)}, UpdateBlockPacket.FLAG_ALL);
}

public static final LinkedHashMap<UUID, Vector3> itemUseVector = new LinkedHashMap<>();

@EventHandler(priority = EventPriority.HIGHEST)
public void onInteract(PlayerInteractEvent event) {
if (event.equals(dealingEvent) || Boolean.TRUE.equals(dealingEvent)) {
Expand All @@ -101,13 +106,14 @@ public void onInteract(PlayerInteractEvent event) {

final Block block = event.getBlock();
final Player player = event.getPlayer();
final Vector3 pos = player.getDirectionVector();

pos.x = Math.abs(pos.x) + block.x;
pos.y = Math.abs(pos.y) + block.y;
pos.z = Math.abs(pos.z) + block.z;

if (event.getAction() == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
final Vector3 pos = itemUseVector.get(player.getUniqueId());

pos.x = Math.abs(pos.x) + block.x;
pos.y = Math.abs(pos.y) + block.y;
pos.z = Math.abs(pos.z) + block.z;

consumeEvent(event,
() -> NativeCallback.onItemUsed((int) block.x, (int) block.y, (int) block.z,
event.getFace().getIndex(), (float) pos.x, (float) pos.y, (float) pos.z, true,
Expand Down Expand Up @@ -479,7 +485,10 @@ public void onCraftItem(CraftItemEvent event) {
long playerUid = EntityMethod.getIdForEntity(event.getPlayer());
int inventorySize = (int) Math.sqrt(grid.getSize());

consumeEvent(event, () -> NativeCallback.onWorkbenchCraft(grid, playerUid, inventorySize));
if(!NativeWorkbench.canRemoveRecipe(event.getTransaction().getRecipe().getId()))
consumeEvent(event, () -> NativeCallback.onWorkbenchCraft(grid, playerUid, inventorySize));
else
event.setCancelled();
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/reider745/hooks/LevelHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public static Item useBreakOn(Level level, Vector3 vector, BlockFace face, Item
return item;
}

@Inject
public static void useItemOn(Level self, Vector3 vector3, Item item, BlockFace face, float fx, float fy, float fz, Player player, boolean sound){
EventListener.itemUseVector.put(player.getUniqueId(), new Vector3(fx, fy, fz));
}

@Inject(className = "cn.nukkit.level.EnumLevel")
public static void initLevels(){
DimensionsMethods.initLevels();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/reider745/hooks/PlayerHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static ConnectedClient getForPlayer(Player player) {
@Inject(type = TypeHook.BEFORE_NOT_REPLACE)
public static void close(Player player, TextContainer message, String reason, boolean notify) {
NetworkHooks.close(player);
EventListener.itemUseVector.remove(player.getUniqueId());
ConnectedClient client = getForPlayer(player);
try {
if (client != null)
Expand Down
61 changes: 33 additions & 28 deletions src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeWorkbench.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
package com.zhekasmirnov.apparatus.mcpe;

import cn.nukkit.Server;
import cn.nukkit.inventory.CraftingManager;
import cn.nukkit.inventory.ShapedRecipe;
import cn.nukkit.inventory.ShapelessRecipe;
import cn.nukkit.inventory.*;
import cn.nukkit.item.Item;
import cn.nukkit.item.RuntimeItemMapping;
import cn.nukkit.item.RuntimeItems;

import cn.nukkit.network.protocol.ProtocolInfo;
import com.reider745.InnerCoreServer;
import com.reider745.hooks.ItemUtils;
import com.zhekasmirnov.apparatus.adapter.innercore.game.item.ItemStack;
import com.zhekasmirnov.horizon.runtime.logger.Logger;
import com.zhekasmirnov.innercore.api.NativeItemInstanceExtra;

import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

public class NativeWorkbench {
interface IApply {
void apply(RuntimeItemMapping mapping, CraftingManager craftingManager);
}

private static final ArrayList<UUID> removedRecipes = new ArrayList<>();

public static boolean canRemoveRecipe(UUID uuid){
return removedRecipes.contains(uuid);
}

private static class RecipeRemove implements IApply {
public int resultId, resultCount, resultData;

Expand All @@ -32,35 +39,33 @@ public RecipeRemove(int resultId, int resultCount, int resultData){
this.resultData = resultData;
}

private static Collection<Recipe> getRecipes(CraftingManager craftingManager){
try{
Method method = craftingManager.getClass().getDeclaredMethod("getRegisterRecipes", int.class);
method.setAccessible(true);
return (Collection<Recipe>) method.invoke(craftingManager, InnerCoreServer.RECIPES_PROTOCOL);
}catch (Exception e){
Logger.error("ERROR-REMOVE-RECIPE", e);
}
return null;
}

@Override
public void apply(RuntimeItemMapping mapping, CraftingManager craftingManager) {
Item itemInstance = ItemUtils.get(this.resultId, this.resultCount, this.resultData);
AtomicBoolean canRemove = new AtomicBoolean(false);

craftingManager.getShapelessRecipes(InnerCoreServer.PROTOCOL).forEach((uid, recipes) -> {
for (UUID uuid : recipes.keySet()) {
ShapelessRecipe recipe = recipes.get(uuid);
if (recipe.getResult().equals(itemInstance)) {
recipes.remove(uuid);
canRemove.set(true);
return;
}
final Item itemInstance = ItemUtils.get(this.resultId, this.resultCount, this.resultData);
final Collection<Recipe> recipes = getRecipes(craftingManager);
boolean removeRecipe = false;

// TODO: Удаляет все крафты этого предмета, в том числе ели их несколько
for(Recipe recipe : recipes){
if(recipe instanceof CraftingRecipe craftingRecipe && recipe.getResult().equals(itemInstance)){
removedRecipes.add(craftingRecipe.getId());
removeRecipe = true;
}
});

craftingManager.getShapedRecipes(InnerCoreServer.PROTOCOL).forEach((uid, recipes) -> {
for (UUID uuid : recipes.keySet()) {
ShapedRecipe recipe = recipes.get(uuid);
if (recipe.getResult().equals(itemInstance)) {
recipes.remove(uuid);
canRemove.set(true);
return;
}
}
});
}

if(!canRemove.get())
Logger.warning("Not remove recipe "+itemInstance);
if(!removeRecipe)
Logger.warning("Not remove recipe " + itemInstance);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/innercore/coreengine/core-engine.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ var TileEntity = {
tileEntity.container.close();
if (tileEntity.container.isLegacyContainer()) {
tileEntity.container.dropAt(tileEntity.x + 0.5, tileEntity.y + 0.5, tileEntity.z + 0.5);
} else {
} else if(tileEntity.blockSource) {
tileEntity.container.dropAt(tileEntity.blockSource, tileEntity.x + 0.5, tileEntity.y + 0.5, tileEntity.z + 0.5);
tileEntity.container.removeEntity();
}
Expand Down

0 comments on commit 0a0009a

Please sign in to comment.