mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-17 17:44:03 +00:00
Rewrite session code and add support for persistent sessions.
This commit is contained in:
@@ -27,6 +27,7 @@ import com.sk89q.worldedit.extension.platform.AbstractPlayerActor;
|
||||
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
||||
import com.sk89q.worldedit.internal.LocalWorldAdapter;
|
||||
import com.sk89q.worldedit.internal.cui.CUIEvent;
|
||||
import com.sk89q.worldedit.session.SessionKey;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -34,6 +35,7 @@ import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.util.ChatMessageComponent;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ForgePlayer extends AbstractPlayerActor {
|
||||
|
||||
@@ -41,13 +43,21 @@ public class ForgePlayer extends AbstractPlayerActor {
|
||||
|
||||
protected ForgePlayer(EntityPlayerMP player) {
|
||||
this.player = player;
|
||||
ThreadSafeCache.getInstance().getOnlineIds().add(getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return player.getUniqueID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemInHand() {
|
||||
ItemStack is = this.player.getCurrentEquippedItem();
|
||||
return is == null ? 0 : is.itemID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.player.username;
|
||||
}
|
||||
@@ -67,26 +77,32 @@ public class ForgePlayer extends AbstractPlayerActor {
|
||||
this.player.cameraPitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorldVector getPosition() {
|
||||
return new WorldVector(LocalWorldAdapter.adapt(ForgeWorldEdit.inst.getWorld(this.player.worldObj)), this.player.posX, this.player.posY, this.player.posZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public com.sk89q.worldedit.world.World getWorld() {
|
||||
return ForgeWorldEdit.inst.getWorld(this.player.worldObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getPitch() {
|
||||
return this.player.rotationPitch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getYaw() {
|
||||
return this.player.rotationYaw;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void giveItem(int type, int amt) {
|
||||
this.player.inventory.addItemStackToInventory(new ItemStack(type, amt, 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatchCUIEvent(CUIEvent event) {
|
||||
String[] params = event.getParameters();
|
||||
String send = event.getTypeId();
|
||||
@@ -97,42 +113,50 @@ public class ForgePlayer extends AbstractPlayerActor {
|
||||
this.player.playerNetServerHandler.sendPacketToPlayer(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printRaw(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
this.player.sendChatToPlayer(ChatMessageComponent.createFromText(part));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printDebug(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
this.player.sendChatToPlayer(ChatMessageComponent.createFromText("\u00a77" + part));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
this.player.sendChatToPlayer(ChatMessageComponent.createFromText("\u00a7d" + part));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printError(String msg) {
|
||||
for (String part : msg.split("\n")) {
|
||||
this.player.sendChatToPlayer(ChatMessageComponent.createFromText("\u00a7c" + part));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector pos, float pitch, float yaw) {
|
||||
this.player.playerNetServerHandler.setPlayerLocation(pos.getX(), pos.getY(), pos.getZ(), pitch, yaw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroups() {
|
||||
return new String[]{}; // WorldEditMod.inst.getPermissionsResolver().getGroups(this.player.username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockBag getInventoryBlockBag() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String perm) {
|
||||
return ForgeUtil.hasPermission(this.player, perm);
|
||||
}
|
||||
@@ -143,4 +167,45 @@ public class ForgePlayer extends AbstractPlayerActor {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionKey getSessionKey() {
|
||||
return new SessionKeyImpl(player.getUniqueID(), player.username);
|
||||
}
|
||||
|
||||
private static class SessionKeyImpl implements SessionKey {
|
||||
// If not static, this will leak a reference
|
||||
|
||||
private final UUID uuid;
|
||||
private final String name;
|
||||
|
||||
private SessionKeyImpl(UUID uuid, String name) {
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
// We can't directly check if the player is online because
|
||||
// the list of players is not thread safe
|
||||
return ThreadSafeCache.getInstance().getOnlineIds().contains(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPersistent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -32,8 +32,15 @@ import cpw.mods.fml.common.FMLLog;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.Mod.EventHandler;
|
||||
import cpw.mods.fml.common.Mod.Instance;
|
||||
import cpw.mods.fml.common.event.*;
|
||||
import cpw.mods.fml.common.event.FMLInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerAboutToStartEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStartedEvent;
|
||||
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
|
||||
import cpw.mods.fml.common.network.NetworkMod;
|
||||
import cpw.mods.fml.common.registry.TickRegistry;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
@@ -84,6 +91,8 @@ public class ForgeWorldEdit {
|
||||
|
||||
config = new ForgeConfiguration(this);
|
||||
config.load();
|
||||
|
||||
TickRegistry.registerTickHandler(ThreadSafeCache.getInstance(), Side.SERVER);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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 Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.forge;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.ITickHandler;
|
||||
import cpw.mods.fml.common.TickType;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
* Caches data that cannot be accessed from another thread safely.
|
||||
*/
|
||||
class ThreadSafeCache implements ITickHandler {
|
||||
|
||||
private static final long REFRESH_DELAY = 1000 * 30;
|
||||
private static final ThreadSafeCache INSTANCE = new ThreadSafeCache();
|
||||
private Set<UUID> onlineIds = Collections.emptySet();
|
||||
private long lastRefresh = 0;
|
||||
|
||||
/**
|
||||
* Get an concurrent-safe set of UUIDs of online players.
|
||||
*
|
||||
* @return a set of UUIDs
|
||||
*/
|
||||
public Set<UUID> getOnlineIds() {
|
||||
return onlineIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tickStart(EnumSet<TickType> type, Object... tickData) {
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
if (now - lastRefresh > REFRESH_DELAY) {
|
||||
Set<UUID> onlineIds = new HashSet<UUID>();
|
||||
|
||||
for (Object object : FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().playerEntityList) {
|
||||
if (object != null) {
|
||||
EntityPlayerMP player = (EntityPlayerMP) object;
|
||||
onlineIds.add(player.getUniqueID());
|
||||
}
|
||||
}
|
||||
|
||||
this.onlineIds = new CopyOnWriteArraySet<UUID>(onlineIds);
|
||||
|
||||
lastRefresh = now;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumSet<TickType> ticks() {
|
||||
return EnumSet.of(TickType.SERVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "WorldEdit Cache";
|
||||
}
|
||||
|
||||
public static ThreadSafeCache getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user