Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
balugaq committed Nov 24, 2024
1 parent 0139bac commit 2574447
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/main/java/com/ytdd9527/networksexpansion/utils/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,50 @@

import java.lang.reflect.Field;

@Deprecated
public class WorldUtils {
protected static Class<?> CraftBlockStateClass;
protected static Field IBlockDataField;
protected static Field BlockPositionField;
protected static Field WorldField;
protected static Field WeakWorldField;
protected static boolean invokeBlockStateSuccess = false;
protected static Class<?> craftBlockStateClass;
protected static Field interfaceBlockDataField;
protected static Field blockPositionField;
protected static Field worldField;
protected static Field weakWorldField;
protected static boolean success = false;

static {
try {
World sampleWorld = Bukkit.getWorlds().get(0);
BlockState blockstate = sampleWorld.getBlockAt(0, 0, 0).getState();
var result = ReflectionUtil.getDeclaredFieldsRecursively(blockstate.getClass(), "data");
IBlockDataField = result.getFirstValue();
IBlockDataField.setAccessible(true);
CraftBlockStateClass = result.getSecondValue();
BlockPositionField = ReflectionUtil.getDeclaredFieldsRecursively(CraftBlockStateClass, "position").getFirstValue();
BlockPositionField.setAccessible(true);
WorldField = ReflectionUtil.getDeclaredFieldsRecursively(CraftBlockStateClass, "world").getFirstValue();
WorldField.setAccessible(true);
WeakWorldField = ReflectionUtil.getDeclaredFieldsRecursively(CraftBlockStateClass, "weakWorld").getFirstValue();
WeakWorldField.setAccessible(true);
invokeBlockStateSuccess = true;
interfaceBlockDataField = result.getFirstValue();
interfaceBlockDataField.setAccessible(true);
craftBlockStateClass = result.getSecondValue();
blockPositionField = ReflectionUtil.getDeclaredFieldsRecursively(craftBlockStateClass, "position").getFirstValue();
blockPositionField.setAccessible(true);
worldField = ReflectionUtil.getDeclaredFieldsRecursively(craftBlockStateClass, "world").getFirstValue();
worldField.setAccessible(true);
weakWorldField = ReflectionUtil.getDeclaredFieldsRecursively(craftBlockStateClass, "weakWorld").getFirstValue();
weakWorldField.setAccessible(true);
success = true;
} catch (Throwable ignored) {

}
}

public static boolean copyBlockState(BlockState stateToSet, Block toBlock) {
if (!invokeBlockStateSuccess) {
public static boolean copyBlockState(BlockState fromBlockState, Block toBlock) {
if (!success) {
return false;
}

BlockState originalState = toBlock.getState();
if (!CraftBlockStateClass.isInstance(originalState) || !CraftBlockStateClass.isInstance(stateToSet)) {
BlockState toState = toBlock.getState();
if (!craftBlockStateClass.isInstance(toState) || !craftBlockStateClass.isInstance(fromBlockState)) {
return false;
}

try {
BlockPositionField.set(stateToSet, BlockPositionField.get(originalState));
WorldField.set(stateToSet, WorldField.get(originalState));
WeakWorldField.set(stateToSet, WeakWorldField.get(originalState));
stateToSet.update(true, false);
blockPositionField.set(fromBlockState, blockPositionField.get(toState));
worldField.set(fromBlockState, worldField.get(toState));
weakWorldField.set(fromBlockState, weakWorldField.get(toState));
fromBlockState.update(true, false);
return true;
} catch (Throwable ignored) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@

@SuppressWarnings({"deprecation", "unused"})
public class NetworksMain implements TabExecutor {
@Deprecated
private static final Set<UUID> requesters = new ConcurrentSkipListSet<>();
@Deprecated
private static final Networks javaPlugin = Networks.getInstance();
@Deprecated
private static final Map<UUID, Pair<Location, Location>> SELECTED_POS = new HashMap<>();

public NetworksMain() {
Expand All @@ -87,6 +90,7 @@ public NetworksMain() {
}, 0, Slimefun.getTickerTask().getTickRate());
}

@Deprecated
public static Location getPos1(Player p) {
if (SELECTED_POS.get(p.getUniqueId()) == null) {
return null;
Expand All @@ -95,26 +99,31 @@ public static Location getPos1(Player p) {
return SELECTED_POS.get(p.getUniqueId()).getFirstValue();
}

@Deprecated
public static Location getPos2(Player p) {
if (SELECTED_POS.get(p.getUniqueId()) == null) {
return null;
}
return SELECTED_POS.get(p.getUniqueId()).getSecondValue();
}

@Deprecated
public static void setPos1(Player p, Location pos) {
SELECTED_POS.put(p.getUniqueId(), new Pair<>(pos, getPos2(p)));
}

@Deprecated
public static void setPos2(Player p, Location pos) {
SELECTED_POS.put(p.getUniqueId(), new Pair<>(getPos1(p), pos));
}

@Deprecated
public static void clearPos(Player p) {
SELECTED_POS.remove(p.getUniqueId());
p.sendMessage(Networks.getLocalizationService().getString("messages.commands.clear-selected-pos"));
}

@Deprecated
public static void toggleShowSelectedAreaOutline(Player p) {
if (requesters.contains(p.getUniqueId())) {
requesters.remove(p.getUniqueId());
Expand All @@ -125,6 +134,7 @@ public static void toggleShowSelectedAreaOutline(Player p) {
}
}

@Deprecated
private static void handleSelectedAreaOutlineShowRequest(Player p) {
Location pos1 = getPos1(p);
Location pos2 = getPos2(p);
Expand All @@ -135,6 +145,7 @@ private static void handleSelectedAreaOutlineShowRequest(Player p) {
javaPlugin.getServer().getScheduler().runTaskLaterAsynchronously(javaPlugin, () -> ParticleUtil.drawRegionOutline(javaPlugin, Particle.WAX_OFF, 0, pos1, pos2), Slimefun.getTickerTask().getTickRate());
}

@Deprecated
public static String locationToString(Location l) {
if (l == null) {
return Networks.getLocalizationService().getString("icons.drawer.location_error.unknown");
Expand All @@ -145,6 +156,7 @@ public static String locationToString(Location l) {
return l.getWorld().getName() + "," + l.getBlockX() + "," + l.getBlockY() + "," + l.getBlockZ();
}

@Deprecated
public static long locationRange(Location pos1, Location pos2) {
if (pos1 == null || pos2 == null) {
return 0;
Expand All @@ -159,6 +171,7 @@ public static long locationRange(Location pos1, Location pos2) {
return (long) (Math.abs(upX - downX) + 1) * (Math.abs(upY - downY) + 1) * (Math.abs(upZ - downZ) + 1);
}

@Deprecated
private static void doWorldEdit(Location pos1, Location pos2, Consumer<Location> consumer) {
if (pos1 == null || pos2 == null) {
return;
Expand Down Expand Up @@ -368,6 +381,7 @@ public static void setContainerId(Player player, int containerId) {
}


@Deprecated
public static void worldeditPos1(Player player) {
Block targetBlock = player.getTargetBlockExact(8, FluidCollisionMode.NEVER);
if (targetBlock == null) {
Expand All @@ -377,6 +391,7 @@ public static void worldeditPos1(Player player) {
worldeditPos1(player, targetBlock.getLocation());
}

@Deprecated
public static void worldeditPos1(Player player, Location location) {
setPos1(player, location);
if (getPos2(player) == null) {
Expand All @@ -386,6 +401,7 @@ public static void worldeditPos1(Player player, Location location) {
}
}

@Deprecated
public static void worldeditPos2(Player player) {
Block targetBlock = player.getTargetBlockExact(8, FluidCollisionMode.NEVER);
if (targetBlock == null) {
Expand All @@ -395,6 +411,7 @@ public static void worldeditPos2(Player player) {
worldeditPos2(player, targetBlock.getLocation());
}

@Deprecated
public static void worldeditPos2(Player player, Location location) {
setPos2(player, location);
if (getPos1(player) == null) {
Expand All @@ -404,10 +421,12 @@ public static void worldeditPos2(Player player, Location location) {
}
}

@Deprecated
public static void worldeditClone(Player player) {
worldeditClone(player, false);
}

@Deprecated
public static void worldeditClone(Player player, boolean overrideData) {
if (getPos1(player) == null || getPos2(player) == null) {
player.sendMessage(Networks.getLocalizationService().getString("messages.commands.worldedit.must-select-range"));
Expand Down Expand Up @@ -518,14 +537,17 @@ public static void worldeditClone(Player player, boolean overrideData) {
});
}

@Deprecated
public static void worldeditPaste(Player player, String sfid) {
worldeditPaste(player, sfid, false, false);
}

@Deprecated
public static void worldeditPaste(Player player, String sfid, boolean overrideData) {
worldeditPaste(player, sfid, overrideData, false);
}

@Deprecated
public static void worldeditPaste(Player player, String sfid, boolean overrideData, boolean force) {
final SlimefunItem sfItem = SlimefunItem.getById(sfid);

Expand Down Expand Up @@ -609,6 +631,7 @@ public static void worldeditPaste(Player player, String sfid, boolean overrideDa
player.sendMessage(String.format(Networks.getLocalizationService().getString("messages.commands.worldedit.paste-done"), count, System.currentTimeMillis() - currentMillSeconds));
}

@Deprecated
public static void worldeditClear(Player player, boolean callHandler, boolean skipVanilla) {
if (getPos1(player) == null || getPos2(player) == null) {
player.sendMessage(Networks.getLocalizationService().getString("messages.commands.worldedit.must-select-range"));
Expand Down Expand Up @@ -647,6 +670,7 @@ public static void worldeditClear(Player player, boolean callHandler, boolean sk
player.sendMessage(String.format(Networks.getLocalizationService().getString("messages.commands.worldedit.clear-done"), count, System.currentTimeMillis() - currentMillSeconds));
}

@Deprecated
public static void worldeditBlockMenuSetSlot(Player player, int slot) {
if (getPos1(player) == null || getPos2(player) == null) {
player.sendMessage(Networks.getLocalizationService().getString("messages.commands.worldedit.must-select-range"));
Expand Down Expand Up @@ -681,6 +705,7 @@ public static void worldeditBlockMenuSetSlot(Player player, int slot) {
player.sendMessage(String.format(Networks.getLocalizationService().getString("messages.commands.worldedit.set-slot-done"), slot, itemName, System.currentTimeMillis() - currentMillSeconds));
}

@Deprecated
public static void worldeditBlockInfoAdd(Player player, String key, String value) {
if (getPos1(player) == null || getPos2(player) == null) {
player.sendMessage(Networks.getLocalizationService().getString("messages.commands.worldedit.must-select-range"));
Expand All @@ -706,6 +731,7 @@ public static void worldeditBlockInfoAdd(Player player, String key, String value
player.sendMessage(String.format(Networks.getLocalizationService().getString("messages.commands.worldedit.set-info-done"), key, value, System.currentTimeMillis() - currentMillSeconds));
}

@Deprecated
public static void worldeditBlockInfoRemove(Player player, String key) {
if (getPos1(player) == null || getPos2(player) == null) {
player.sendMessage(Networks.getLocalizationService().getString("messages.commands.worldedit.must-select-range"));
Expand Down

0 comments on commit 2574447

Please sign in to comment.