Last active
May 24, 2021 13:35
-
-
Save Aaron1011/d727315aa9b1ba5a4b1b to your computer and use it in GitHub Desktop.
Pixelmon Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.aaron1011.pixelplugin; | |
| import com.google.common.collect.Maps; | |
| import com.pixelmonmod.pixelmon.Pixelmon; | |
| import com.pixelmonmod.pixelmon.achievement.PixelmonAchievements; | |
| import com.pixelmonmod.pixelmon.api.events.PixelmonCaptureEvent; | |
| import com.pixelmonmod.pixelmon.api.events.PixelmonRecievedEvent; | |
| import com.pixelmonmod.pixelmon.api.events.ReceiveType; | |
| import com.pixelmonmod.pixelmon.battles.BattleRegistry; | |
| import com.pixelmonmod.pixelmon.comm.CommandChatHandler; | |
| import com.pixelmonmod.pixelmon.config.PixelmonEntityList; | |
| import com.pixelmonmod.pixelmon.config.PixelmonItemsPokeballs; | |
| import com.pixelmonmod.pixelmon.entities.pixelmon.EntityPixelmon; | |
| import com.pixelmonmod.pixelmon.enums.EnumPokeballs; | |
| import com.pixelmonmod.pixelmon.enums.EnumPokemon; | |
| import com.pixelmonmod.pixelmon.storage.PixelmonStorage; | |
| import com.pixelmonmod.pixelmon.storage.PlayerNotLoadedException; | |
| import net.minecraft.entity.player.EntityPlayer; | |
| import net.minecraft.entity.player.EntityPlayerMP; | |
| import net.minecraft.item.ItemStack; | |
| import net.minecraft.util.StatCollector; | |
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
| import org.spongepowered.api.entity.player.Player; | |
| import org.spongepowered.api.event.Subscribe; | |
| import org.spongepowered.api.event.entity.player.PlayerJoinEvent; | |
| import org.spongepowered.api.event.state.ServerStartedEvent; | |
| import org.spongepowered.api.plugin.Plugin; | |
| import org.spongepowered.api.scoreboard.Scoreboard; | |
| import org.spongepowered.api.scoreboard.critieria.Criteria; | |
| import org.spongepowered.api.scoreboard.displayslot.DisplaySlots; | |
| import org.spongepowered.api.scoreboard.objective.Objective; | |
| import org.spongepowered.api.text.Text; | |
| import org.spongepowered.api.text.Texts; | |
| import org.spongepowered.api.text.format.TextColors; | |
| import org.spongepowered.mod.SpongeMod; | |
| import java.util.Map; | |
| import java.util.Random; | |
| import java.util.UUID; | |
| @Plugin(id = PixelPlugin.MODID, name = PixelPlugin.NAME, version = PixelPlugin.VERSION, dependencies = "required-after:pixelmon") | |
| public class PixelPlugin { | |
| public static final String MODID = "pixelplugn"; | |
| public static final String NAME = "PixelPlugin"; | |
| public static final String VERSION = "1.0.0"; | |
| private static final String OBJECTIVE_NAME = "sidebarObj"; | |
| private static Text OBJECTIVE_DISPLAY_NAME; | |
| private static final EnumPokemon[] POKEMON_GIFT_TYPES = new EnumPokemon[] {EnumPokemon.Lugia, EnumPokemon.Mewtwo}; | |
| private static final int CAPTURE_COUNT = 3; | |
| public static Pixelmon pixelmonInstance; | |
| private static final Random RANDOM = new Random(); | |
| private final Map<UUID, Integer> captures = Maps.newHashMap(); | |
| @Subscribe | |
| public void onServerStarted(ServerStartedEvent event) { | |
| pixelmonInstance.EVENT_BUS.register(this); | |
| OBJECTIVE_DISPLAY_NAME = Texts.of(TextColors.GREEN, "Captures"); | |
| pixelmonInstance = (Pixelmon) event.getGame().getPluginManager().getPlugin("pixelmon").get().getInstance(); | |
| } | |
| @Subscribe | |
| public void onPlayerJoin(PlayerJoinEvent event) { | |
| if (!this.captures.containsKey(event.getEntity())) { | |
| this.captures.put(event.getEntity().getUniqueId(), 0); | |
| } | |
| this.createObjective(event.getEntity()); | |
| this.addCurrentScore(event.getEntity()); | |
| } | |
| @SubscribeEvent | |
| public void onCatch(PixelmonCaptureEvent event) { | |
| Player player = (Player) event.player; | |
| this.updateScore(player); | |
| if (this.getCapturedPokemon(player) == CAPTURE_COUNT) { | |
| this.giftPlayer(player); | |
| } | |
| } | |
| private void giftPlayer(Player player) { | |
| EntityPlayer entityPlayer = (EntityPlayer) player; | |
| this.givePokemon(entityPlayer, POKEMON_GIFT_TYPES[RANDOM.nextInt(POKEMON_GIFT_TYPES.length)]); | |
| entityPlayer.inventory.addItemStackToInventory(new ItemStack(PixelmonItemsPokeballs.masterBall)); | |
| } | |
| private void givePokemon(EntityPlayer player, EnumPokemon pokemonType) { | |
| try { | |
| if (BattleRegistry.getBattle(player) != null) { | |
| CommandChatHandler.sendChat(player, "pixelmon.command.general.inbattle"); | |
| return; | |
| } | |
| EntityPixelmon pokemon = (EntityPixelmon) PixelmonEntityList.createEntityByName(pokemonType.name, player.worldObj); | |
| pokemon.caughtBall = EnumPokeballs.MasterBall; | |
| pokemon.friendship.initFromCapture(); | |
| PixelmonStorage.PokeballManager.getPlayerStorage((EntityPlayerMP) player).addToParty(pokemon); | |
| PixelmonAchievements.pokedexChieves(player); | |
| Pixelmon.EVENT_BUS.post(new PixelmonRecievedEvent(player, ReceiveType.Command, pokemon)); | |
| CommandChatHandler.sendChat(player, "pixelmon.command.give.givesuccess", player.getDisplayName().getUnformattedText(), | |
| StatCollector.translateToLocal("pixelmon." + pokemon.getName().toLowerCase() + ".name")); | |
| } catch (PlayerNotLoadedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private void updateScore(Player player) { | |
| this.removeCurrentScore(player); | |
| this.captures.put(player.getUniqueId(), this.getCapturedPokemon(player) + 1); | |
| this.addCurrentScore(player); | |
| } | |
| private void addCurrentScore(Player player) { | |
| int captured = this.getCapturedPokemon(player); | |
| Objective objective = player.getScoreboard().getObjective(OBJECTIVE_NAME).get(); | |
| objective.getScore(this.getName(captured)).setScore(captured); | |
| } | |
| private void removeCurrentScore(Player player) { | |
| Objective objective = player.getScoreboard().getObjective(OBJECTIVE_NAME).get(); | |
| objective.removeScore(objective.getScore(this.getName(this.getCapturedPokemon(player)))); | |
| } | |
| private int getCapturedPokemon(Player player) { | |
| return this.captures.get(player.getUniqueId()); | |
| } | |
| private void createObjective(Player player) { | |
| Scoreboard scoreboard = SpongeMod.instance.getSpongeRegistry().getScoreboardBuilder().build(); | |
| Objective objective = SpongeMod.instance.getSpongeRegistry().getObjectiveBuilder().name(OBJECTIVE_NAME).displayName(OBJECTIVE_DISPLAY_NAME).criterion( | |
| Criteria.DUMMY).build(); | |
| scoreboard.addObjective(objective); | |
| scoreboard.addObjective(objective, DisplaySlots.SIDEBAR); | |
| player.setScoreboard(scoreboard); | |
| } | |
| private Text getName(int score) { | |
| return Texts.of(TextColors.AQUA, String.format("Pokemon captured: %d/%d", score, CAPTURE_COUNT)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment