Exemplo n.º 1
0
 def __init__(self, api, log):
     self.api = api
     self.minecraft = api.minecraft
     self.log = log
     self.listener = Listen()
     self.listening = False
Exemplo n.º 2
0
class Main:
    def __init__(self, api, log):
        self.api = api
        self.minecraft = api.minecraft
        self.log = log
        self.listener = Listen()
        self.listening = False

    def onEnable(self):
        self.listener.makenewdir(self.listener.vote_log_path)  # Make sure path exists
        self.listener.votipyer_running = True  # set listener to run mode
        t1 = Thread(target=self.listener.startlistener, args=())
        t1.daemon = True
        t1.start()  # start the listener as a thread
        self.listening = True

        t2 = Thread(target=self.enablevoting, args=())
        t2.daemon = True
        t2.start()  # start the thread to listen for individual connections

    def onDisable(self):
        self.listener.votipyer_running = False # shut off the votipyer connection
        self.listening = False

    def enablevoting(self):
        """trying to run a loop like this from one of the wrapper events will hang Wrapper.
        That's why this method is called as a thread. If the reward code is complex, you may
        want to spur off that code to yet another thread so that the next connection can be
        processed."""
        while self.listening:
            vote = self.listener.getvote()
            if vote[0] == "VOTE":
                rt = Thread(target=self.reward_thread, args=[vote])
                rt.daemon = True
                rt.start()
            if vote[0] == "FAIL" and "No socket conn" in vote[1]:  # this is an important check
                self.log.error("Socket Connection not present - closing Listener")
                print("Socket Connection not present - closing Listener")
                self.listener.votipyer_running = False
                self.listening = False

    def reward_thread(self, vote):
        # we made sure 'self.listener.votelogpath' is a valid path in the first line of onEnable()
        with open("%s/vote.log" % self.listener.vote_log_path, 'a') as f:
            f.write(str(vote) + "\n")
        onlineplayers = self.api.minecraft.getPlayers()
        if vote[2] in onlineplayers:
            rewardplayer = vote[2]
            self.api.minecraft.console("say %s gets a diamond for voting for us on %s." % (rewardplayer, vote[1]))
            self.api.minecraft.console("give %s minecraft:diamond" % rewardplayer)
            self.api.minecraft.console("say Everyone now gets this potion effect:")
            if "PlanetMinecraft" in vote[1]:
                self.api.minecraft.console("say Speed for 1 minute!")
                self.api.minecraft.console("effect @a 1 60 3")
            elif "MCSL" in vote[1]:
                self.api.minecraft.console("say Haste for 1 minute... Go mine stuff!")
                self.api.minecraft.console("effect @a 3 60 3")
            else:
                self.api.minecraft.console("say Healing for everyone!")
                self.api.minecraft.console("/effect @a 6 60 20")
        else:
            self.api.minecraft.console("say %s Voted for us offline on %s." % (vote[2], vote[1]))