/* This file is a part of the SteamWar software. Copyright (C) 2021 SteamWar.de-Serverteam This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.steamwar.fightsystem.utils; import com.comphenix.tinyprotocol.Reflection; import de.steamwar.core.Core; import de.steamwar.fightsystem.Config; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; import java.util.Map; public class BlockIdWrapper14 implements BlockIdWrapper { private static final Class chunkProviderServer = Reflection.getClass("{nms.server.level}.ChunkProviderServer"); private static final Reflection.MethodInvoker getChunkProvider = Reflection.getTypedMethod(worldServer, null, chunkProviderServer); private static final Class playerChunkMap = Reflection.getClass("{nms.server.level}.PlayerChunkMap"); private static final Reflection.FieldAccessor getPlayerChunkMap = Reflection.getField(chunkProviderServer, playerChunkMap, 0); private static final Reflection.FieldAccessor entityTrackers = Core.getVersion() > 15 ? Reflection.getField(playerChunkMap, Int2ObjectMap.class, 0) : Reflection.getField(playerChunkMap, org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectMap.class, 0); private static final Class block = Reflection.getClass("{nms.world.level.block}.Block"); private static final Class iBlockData = Reflection.getClass("{nms.world.level.block.state}.IBlockData"); private static final Class blockPosition = Reflection.getClass("{nms.core}.BlockPosition"); private final Map trackers; public BlockIdWrapper14() { trackers = entityTrackers.get(getPlayerChunkMap.get(getChunkProvider.invoke(getWorldHandle.invoke(Config.world)))); } private static final Reflection.MethodInvoker getCombinedId = Reflection.getTypedMethod(block, null, int.class, iBlockData); private static final Reflection.MethodInvoker getNMS = Reflection.getTypedMethod(Reflection.getClass("{obc}.block.CraftBlock"), "getNMS", iBlockData); @Override public int blockToId(Block block) { return (int) getCombinedId.invoke(null, getNMS.invoke(block)); } private static final Reflection.MethodInvoker getByCombinedId = Reflection.getTypedMethod(block, null, iBlockData, int.class); private static final Reflection.ConstructorInvoker newBlockPosition = Reflection.getConstructor(blockPosition, int.class, int.class, int.class); private static final Reflection.MethodInvoker getTypeAndData = Reflection.getMethod(worldServer, null, blockPosition, iBlockData, int.class); private static final Reflection.MethodInvoker removeTileEntity = Reflection.getMethod(worldServer, Core.getVersion() > 15 ? "m" : "removeTileEntity", blockPosition); private static final Reflection.MethodInvoker flagDirty = Reflection.getMethod(chunkProviderServer, null, blockPosition); @Override public void setBlock(World world, int x, int y, int z, int blockState) { Object blockData = getByCombinedId.invoke(null, blockState); Object nworld = getWorldHandle.invoke(world); Object pos = newBlockPosition.invoke(x, y, z); removeTileEntity.invoke(nworld, pos); getTypeAndData.invoke(nworld, pos, blockData, 1042); flagDirty.invoke(getChunkProvider.invoke(nworld), pos); } private static final Class entityTracker = Reflection.getClass("{nms.server.level}.PlayerChunkMap$EntityTracker"); private static final Reflection.MethodInvoker updatePlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "b" : "updatePlayer", entityPlayer); @Override public void trackEntity(Player player, int entity) { Object tracker = trackers.get(entity); if(tracker != null) updatePlayer.invoke(tracker, getPlayer.invoke(player)); } private static final Reflection.MethodInvoker clearPlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "a" : "clear", entityPlayer); @Override public void untrackEntity(Player player, int entity) { Object tracker = trackers.get(entity); if(tracker != null) clearPlayer.invoke(tracker, getPlayer.invoke(player)); } private static final Reflection.MethodInvoker getMaterialByBlock = Reflection.getTypedMethod(Reflection.getClass("{obc}.util.CraftMagicNumbers"), "getMaterial", Material.class, block); private static final Reflection.MethodInvoker getBlockByBlockData = Reflection.getTypedMethod(iBlockData, null, block); @Override public Material idToMaterial(int blockState) { return (Material)getMaterialByBlock.invoke(null, getBlockByBlockData.invoke(getByCombinedId.invoke(null, blockState))); } }