返回扩展类 [英] Returning an extended class

查看:85
本文介绍了返回扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否真的有意义(我有点困惑自己),但是我想做的是创建一个迷你游戏插件,并且我试图使其在地图之间循环,我创建一个扩展BaseGame的新类,我将插件实例,世界名称和xml文件名传递给超类BaseGame,然后basegame类解析xml文件和设置变量中的信息.在扩展BaseGame的类中,我有一些方法,因为大多数地图具有不同的游戏类型,因此我需要针对不同的事件做不同的事情,例如在TDM上,我需要阻止玩家破坏它,因此我想在侦听器中做到这一点

I'm not sure if this will really make any sense (I've kinda confused my self ) but what I am trying to do is create a mini-game plugin and I'm trying to make it cycle between maps, I create a new class that extends BaseGame I pass in plugin instance, world name and xml file name to the super class BaseGame, The basegame class then parses the information from xml file and setup variables. I have some methods in side the class extending BaseGame because most maps have different game types so I need to do different things for different events for example on TDM I need to stop players breaking this so I want to do this in the listener

@EventHandler
public void blockDestroy(BlockBreakEvent event) {
    plugin.mapCycler.getCurrentWorld().onBreakEvent(event);
}

我面临的问题是从MapCycler类返回当前世界,因为所有地图类都有不同的方法,我需要一种以通用方法返回地图类的方法,该方法可以返回所有扩展BaseGame的地图类,但我仍然需要才能访问扩展BaseGame的类中的方法.

The problem I face is returning the current world from the MapCycler class because all map classes have different methods, I need a way to return the map classes in a generic method that can return all map classes that extend BaseGame but I still need to be able to access the methods inside the class extending BaseGame.

如果需要进一步说明,请告诉我.

Please tell me if you need more clarification.

谢谢

在BaseGame类中,将在所有扩展BaseGame的World类中使用通用方法,例如,将在所有扩展BaseGame的类中使用这些方法

In the BaseGame class it is common methods that will be used in all the World Classes extending BaseGame for example these methods will be used in all the classes extending BaseGame

    public String getMapName() {
    return xmlFileReader.getMapName();
}


public String getMapObjective() {
    return xmlFileReader.getMapObjective();
}
    public void resetInventory(Player player) {
    player.getInventory().clear();
    player.getInventory().setArmorContents(null);
    player.setHealth(20);
    player.setFlying(false);
    player.setGameMode(GameMode.SURVIVAL);
    player.setFoodLevel(20);
}

以及在扩展BaseGame类的类中,它将具有与世界相关的独特方法,例如

and in the classes extending the BaseGame class It will have unique method that relate to the world for example

public void blueWin() {
    Bukkit.getServer().broadcastMessage(Messages.colorize("Blue win - nice message in update"));
    for(String player : blueTeamList) {
        Player blue = Bukkit.getServer().getPlayer(player);
        resetInventory(blue);
        Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(plugin.relicWorld.sb.getObjective(plugin.relicWorld.name), 1);//Create Scoreboard create packet
        sendPacket(blue, packet);
        String loc = DatabaseManager.getLastLoc("SELECT * FROM ag_users WHERE user = \'" + blue.getName() + "\'");
        blue.teleport(LocationManager.stringToLoc(loc));
        Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "eco give " + blue.getName() + " 250");   
    }
    for(String player : redTeamList) {
        Player red = Bukkit.getServer().getPlayer(player);
        resetInventory(red);
        Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(plugin.relicWorld.sb.getObjective(plugin.relicWorld.name), 1);//Create Scoreboard create packet
        sendPacket(red, packet);
        String loc = DatabaseManager.getLastLoc("SELECT * FROM ag_users WHERE user = \'" + red.getName() + "\'");
        red.teleport(LocationManager.stringToLoc(loc));
        Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "eco give " + red.getName() + " 50"); 
    }
    Bukkit.unloadWorld(getWorld(), false);
    resetAll();
}

因为它是世界上独一无二的.

because it's unique to the world.

推荐答案

它们的关键是您需要为world对象定义一个统一的接口.然后,您可以调用相同的方法(在基类/接口中定义),并且通过多态性,每个子类可以做出不同的反应

They key is that you need to define a uniform interface for your world object. Then you can call the same method (defined in the base class/interface) and through polymorphism each subclass can react differently

interface World{
    public void onBreakEvent(BlockBreakEvent e);
}

class World1 extends BaseGame implements World{
    public void onBreakEvent(BlockBreakEvent e) {
        System.out.println("Breaking from world 1")
    }
}

class World2 extends BaseGame implements World{
    public void onBreakEvent(BlockBreakEvent  e) {
        System.out.println("Breaking from world 2")
    }
}

现在,当您调用plugin.mapCycler.getCurrentWorld()时,应该返回一个World对象,即,它实现了World接口.每个对象可以做出不同的反应,但是由于它们都共享接口,因此可以将它们视为相同.

Now when you call plugin.mapCycler.getCurrentWorld() that should return a World object, that is, it implements the World interface. Each object can react differently but because they all share the interface, they can be treated as if they were the same.

plugin.mapCycler.getCurrentWorld().onBreakEvent(event);

这篇关于返回扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆