Skip to content

Commit

Permalink
Effect update
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeTheTech committed Dec 1, 2023
1 parent 1272be3 commit 7a3e839
Show file tree
Hide file tree
Showing 15 changed files with 277 additions and 16 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ dependencies {

//protocol-lib
compileOnly ("com.comphenix.protocol:ProtocolLib:5.1.0")

//goldman player data
compileOnly ("lee.code.playerdata:playerdata:1.0.0")
}

repositories {
mavenLocal()
mavenCentral() // You can add more repositories if needed
maven {
name = "protocollib"
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/lee/code/pets/database/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,29 @@ public void closeConnection() {
}
}

private void alterDatabase() {
try {
//effects
petsDao.executeRaw("ALTER TABLE pets ADD COLUMN effect BOOLEAN DEFAULT 1;");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private void updateDatabase() {
try {
final QueryBuilder<PetTable, Integer> queryBuilder = petsDao.queryBuilder();
queryBuilder.where().like("data", "%WITHER,%");
//blaze
final QueryBuilder<PetTable, Integer> blazeBuilder = petsDao.queryBuilder();
blazeBuilder.where().like("data", "%BLAZE,%");
for (PetTable petTable : blazeBuilder.query()) {
petTable.setData(petTable.getData() + ",false");
petsDao.update(petTable);
}

for (PetTable petTable : queryBuilder.query()) {
//vex
final QueryBuilder<PetTable, Integer> vexBuilder = petsDao.queryBuilder();
vexBuilder.where().like("data", "%VEX,%");
for (PetTable petTable : vexBuilder.query()) {
petTable.setData(petTable.getData() + ",false");
petsDao.update(petTable);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/lee/code/pets/database/cache/CachePets.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ public void deletePet(int id) {
playerPetData.removePlayerPet(petTable.getOwner(), id);
petsCache.remove(id);
}

public void setPetEffect(int id, boolean result) {
final PetTable petTable = getPetTable(id);
petTable.setEffect(result);
updatePetDatabase(petTable);
}

public boolean getPetEffect(int id) {
return getPetTable(id).isEffect();
}
}
4 changes: 4 additions & 0 deletions src/main/java/lee/code/pets/database/tables/PetTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ public class PetTable {
@DatabaseField(columnName = "data", canBeNull = false)
private String data;

@DatabaseField(columnName = "effect", canBeNull = false)
private boolean effect;

public PetTable(int id, UUID owner, String data) {
this.id = id;
this.owner = owner;
this.data = data;
this.effect = true;
}
}
1 change: 1 addition & 0 deletions src/main/java/lee/code/pets/lang/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum Lang {
MENU_RENAME_SUCCESSFUL("&aYou successfully renamed your pet to &f{0}&a!"),
MENU_RENAME_MESSAGE("&aPlease type your new pet's name in chat&7:"),
MENU_PET_ITEM_NAME("&e&lPet Name&7:"),
MENU_PET_EFFECT_LORE("&3&lEffect&7: &e{0}\n&3&lAmplifier&7: &e{1}\n&3&lEnabled&7: &e{2}"),
MENU_PET_ITEM_LORE("&f{0}\n \n&e&lActions&7:\n&e» &7Right-Click &eEdit Pet\n&e» &7Left-Click &eSpawn Pet\n&e» &7Sneak-Click &eDelete Pet"),
CAPTURE_SUCCESSFUL("&aYou successfully captured a &3{0}&a!"),
ERROR_RENAME_COMMAND("&cYou can't use commands until you type in a new pet name."),
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/lee/code/pets/menus/menu/PetOptionMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private MenuButton createOptionButton(Player player, Option option) {
final String marking = PetDataUtil.getNextHorseMarking(targetData);
cachePets.updatePetData(petID, PetDataUtil.addNewPetData(entityType, petData, marking, option));
}
case BABY, SADDLE, CHEST, HORNS, ANGRY, STUNG, NECTAR, PUMPKIN, COLLAR, POWERED, DYE -> {
case BABY, SADDLE, CHEST, HORNS, ANGRY, STUNG, NECTAR, PUMPKIN, COLLAR, POWERED, DYE, CHARGED -> {
final String petOption = String.valueOf(!Boolean.parseBoolean(targetData));
cachePets.updatePetData(petID, PetDataUtil.addNewPetData(entityType, petData, petOption, option));
}
Expand Down Expand Up @@ -118,14 +118,25 @@ private MenuButton createOptionButton(Player player, Option option) {
}

private void addInterfaceButtons(Player player) {
addButton(30, new MenuButton()
addButton(29, new MenuButton()
.creator(p -> MenuItem.PET_EFFECT.createPetEffectItem(entityType, pets.getCacheManager().getCachePets().getPetEffect(petID)))
.consumer(e -> {
final CachePets cachePets = pets.getCacheManager().getCachePets();
getMenuSoundManager().playClickSound(player);
cachePets.setPetEffect(petID, !cachePets.getPetEffect(petID));
pets.getPetManager().removeActivePet(player);
clearButtons();
decorate(player);
}));
addButton(31, new MenuButton()
.creator(p -> MenuItem.BACK_MENU.createItem())
.consumer(e -> {
getMenuSoundManager().playClickSound(player);
pets.getMenuManager().openMenu(new PetMenu(pets), player);
}));
addButton(32, new MenuButton()
.creator(p -> MenuItem.SPAWN_PET.createSpawnPetItem(entityType)).consumer(e -> {
addButton(33, new MenuButton()
.creator(p -> MenuItem.SPAWN_PET.createSpawnPetItem(entityType))
.consumer(e -> {
final CachePets cachePets = pets.getCacheManager().getCachePets();
getMenuSoundManager().playClickSound(player);
pets.getPetManager().removeActivePet(player);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/lee/code/pets/menus/menu/menudata/MenuItem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package lee.code.pets.menus.menu.menudata;

import lee.code.pets.lang.Lang;
import lee.code.pets.utils.CoreUtil;
import lee.code.pets.utils.ItemUtil;
import lee.code.pets.utils.PetEffects;
import lombok.AllArgsConstructor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;

@AllArgsConstructor
public enum MenuItem {
Expand All @@ -19,6 +22,7 @@ public enum MenuItem {
CONFIRM(Material.LIME_STAINED_GLASS_PANE, "&2&lConfirm Delete", null, false, false, null),
CANCEL(Material.RED_STAINED_GLASS_PANE, "&c&lCancel Delete", null, false, false, null),
DEACTIVATE_PET(Material.BARRIER, "&c&lDeactivate Pet", null, false, false, null),
PET_EFFECT(Material.POTION, "&d&lPassive Effect", null, true, true, null),
;

private final Material material;
Expand All @@ -42,4 +46,17 @@ public ItemStack createSpawnPetItem(EntityType entityType) {
head.setItemMeta(itemMeta);
return head;
}

public ItemStack createPetEffectItem(EntityType entityType, boolean effect) {
final ItemStack item = ItemUtil.createItem(material, name, createPetEffectLore(entityType, effect), 0, skin);
if (hideItemFlags) ItemUtil.hideItemFlags(item);
if (enchantItem) ItemUtil.enchantItem(item, Enchantment.ARROW_INFINITE, 1);
return item;
}

private String createPetEffectLore(EntityType entityType, boolean effect) {
final String effectColor = effect ? "&2" : "&c";
final PotionEffect potionEffect = PetEffects.valueOf(entityType.name()).getPotionEffect();
return Lang.MENU_PET_EFFECT_LORE.getString(new String[]{CoreUtil.capitalize(potionEffect.getType().getName()), String.valueOf(potionEffect.getAmplifier()), effectColor + CoreUtil.capitalize(String.valueOf(effect))});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum Option {
LEVEL(Material.EXPERIENCE_BOTTLE, "&e&lLevel"),
COLLAR(Material.LEAD, "&e&lCollar"),
POWERED(Material.TNT, "&e&lPowered"),
CHARGED(Material.MAGMA_BLOCK, "&e&lCharged"),
SIZE(Material.BONE_MEAL, "&e&lSize"),
DYE(Material.FIREWORK_STAR, "&e&lDye")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum OptionSelector {
TADPOLE(new String[] {Option.NAME.name()}),
TROPICAL_FISH(new String[] {Option.NAME.name(), Option.VARIANT.name(), Option.BODY_COLOR.name(), Option.PATTERN_COLOR.name()}),
TURTLE(new String[] {Option.NAME.name(), Option.BABY.name()}),
BLAZE(new String[] {Option.NAME.name()}),
BLAZE(new String[] {Option.NAME.name(), Option.CHARGED.name()}),
CAVE_SPIDER(new String[] {Option.NAME.name()}),
CREEPER(new String[] {Option.NAME.name(), Option.POWERED.name()}),
DROWNED(new String[] {Option.NAME.name(), Option.BABY.name()}),
Expand All @@ -71,7 +71,7 @@ public enum OptionSelector {
SKELETON(new String[] {Option.NAME.name()}),
SPIDER(new String[] {Option.NAME.name()}),
STRAY(new String[] {Option.NAME.name()}),
VEX(new String[] {Option.NAME.name()}),
VEX(new String[] {Option.NAME.name(), Option.ANGRY.name()}),
VINDICATOR(new String[] {Option.NAME.name()}),
WARDEN(new String[] {Option.NAME.name()}),
WITCH(new String[] {Option.NAME.name()}),
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/lee/code/pets/pets/PetManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lee.code.pets.pets;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import lee.code.pets.Pets;
import lee.code.pets.database.cache.CachePets;
import lee.code.pets.pets.pet.animal.*;
Expand All @@ -8,20 +9,26 @@
import lee.code.pets.pets.pet.monster.*;
import lee.code.pets.utils.CoreUtil;
import lee.code.pets.utils.PetDataUtil;
import lee.code.pets.utils.PetEffects;
import lee.code.playerdata.PlayerDataAPI;
import net.minecraft.world.entity.Entity;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftEntity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class PetManager {
private final Pets pets;
private final ConcurrentHashMap<Integer, UUID> petTracker = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, Integer> activePetTracker = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, ScheduledTask> petEffectTasks = new ConcurrentHashMap<>();

public PetManager(Pets pets) {
this.pets = pets;
Expand Down Expand Up @@ -112,6 +119,7 @@ private void spawn(Player player, Location location, int id, Entity entity) {
final CraftEntity craftEntity = entity.getBukkitEntity();
addToPetTracker(id, craftEntity.getUniqueId(), player.getUniqueId());
craftEntity.spawnAt(location, CreatureSpawnEvent.SpawnReason.CUSTOM);
startEffectTimer(player.getUniqueId(), craftEntity.getType(), id);
}

private void addToPetTracker(int id, UUID entity, UUID player) {
Expand Down Expand Up @@ -157,6 +165,7 @@ public void removePet(Player player) {
final org.bukkit.entity.Entity entity = player.getWorld().getEntity(getActivePetUUID(activePet));
if (entity != null) entity.remove();
removeFromPetTracker(activePet, player.getUniqueId());
stopEffectTimer(player.getUniqueId());
}

public void capturePet(Player player, org.bukkit.entity.Entity entity) {
Expand All @@ -178,4 +187,24 @@ public boolean canCaptureNewPet(Player player) {
final int count = pets.getCacheManager().getCachePets().getPlayerPetData().getPetCount(player.getUniqueId());
return count + 1 <= getMaxPets(player);
}

private void startEffectTimer(UUID uuid, EntityType entityType, int petID) {
stopEffectTimer(uuid);
if (!pets.getCacheManager().getCachePets().getPetEffect(petID)) return;
petEffectTasks.put(uuid, Bukkit.getAsyncScheduler().runAtFixedRate(pets, scheduledTask -> {
final Player player = PlayerDataAPI.getOnlinePlayer(uuid);
if (player == null) {
stopEffectTimer(uuid);
return;
}
player.getScheduler().run(pets, task -> player.addPotionEffect(PetEffects.valueOf(entityType.name()).getPotionEffect()), null);
}, 0, 3, TimeUnit.SECONDS));
}

public void stopEffectTimer(UUID uuid) {
if (petEffectTasks.containsKey(uuid)) {
petEffectTasks.get(uuid).cancel();
petEffectTasks.remove(uuid);
}
}
}
29 changes: 27 additions & 2 deletions src/main/java/lee/code/pets/pets/pet/monster/BlazePet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
import lee.code.pets.utils.CoreUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.monster.Blaze;
import net.minecraft.world.entity.monster.Monster;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityTargetEvent;

public class BlazePet extends Blaze {
public class BlazePet extends Monster {
private static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(BlazePet.class, EntityDataSerializers.BYTE);

public BlazePet(Player player, String[] data) {
super(EntityType.BLAZE, ((CraftWorld) player.getLocation().getWorld()).getHandle());
Expand All @@ -23,6 +29,7 @@ public BlazePet(Player player, String[] data) {
setCanPickUpLoot(false);
setNoGravity(true);
setMaxUpStep(1.0F);
setCharged(Boolean.parseBoolean(data[2]));
collides = false;
setCustomName(Component.Serializer.fromJson(CoreUtil.serializeColorComponentJson(data[1])));
setTarget(((CraftPlayer) player).getHandle(), EntityTargetEvent.TargetReason.CUSTOM, false);
Expand All @@ -31,6 +38,11 @@ public BlazePet(Player player, String[] data) {
getBrain().removeAllBehaviors();
}

@Override
protected SoundEvent getAmbientSound() {
return SoundEvents.BLAZE_AMBIENT;
}

@Override
protected void registerGoals() {
goalSelector.addGoal(0, new FollowOwnerFlyingGoal(this, 0.4, 5, 10));
Expand All @@ -44,4 +56,17 @@ public void load(CompoundTag compoundTag) {
public boolean save(CompoundTag compoundTag) {
return false;
}

@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(DATA_FLAGS_ID, (byte) 0);
}

private void setCharged(boolean fireActive) {
byte b = entityData.get(DATA_FLAGS_ID);
if (fireActive) b = (byte)(b | 1);
else b = (byte)(b & -2);
entityData.set(DATA_FLAGS_ID, b);
}
}
23 changes: 23 additions & 0 deletions src/main/java/lee/code/pets/pets/pet/monster/VexPet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lee.code.pets.utils.CoreUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EntityType;
Expand All @@ -15,6 +18,7 @@
import org.bukkit.event.entity.EntityTargetEvent;

public class VexPet extends Mob {
protected static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(VexPet.class, EntityDataSerializers.BYTE);

public VexPet(Player player, String[] data) {
super(EntityType.VEX, ((CraftWorld) player.getLocation().getWorld()).getHandle());
Expand All @@ -26,6 +30,7 @@ public VexPet(Player player, String[] data) {
setNoGravity(true);
setMaxUpStep(1.0F);
collides = false;
setIsCharging(Boolean.parseBoolean(data[2]));
setCustomName(Component.Serializer.fromJson(CoreUtil.serializeColorComponentJson(data[1])));
setTarget(((CraftPlayer) player).getHandle(), EntityTargetEvent.TargetReason.CUSTOM, false);
moveControl = new ControllerWASDFlying(this, player.getUniqueId(), 0.3F);
Expand All @@ -51,4 +56,22 @@ public void load(CompoundTag compoundTag) {
public boolean save(CompoundTag compoundTag) {
return false;
}

@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(DATA_FLAGS_ID, (byte) 0);
}

private void setVexFlag(int mask, boolean value) {
final byte b0 = entityData.get(DATA_FLAGS_ID);
final int j;
if (value) j = b0 | mask;
else j = b0 & ~mask;
entityData.set(DATA_FLAGS_ID, (byte) (j & 255));
}

public void setIsCharging(boolean charging) {
setVexFlag(1, charging);
}
}
Loading

0 comments on commit 7a3e839

Please sign in to comment.