Track progress through int, part 1. See #2246

This commit is contained in:
PikaMug 2024-07-08 09:56:30 -04:00
parent 8f89eb9d8c
commit e24b364297
6 changed files with 144 additions and 156 deletions

View File

@ -40,6 +40,7 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
public class BukkitBlockListener implements Listener {
@ -103,16 +104,16 @@ public class BukkitBlockListener implements Listener {
if (currentStage.containsObjective(placeType)) {
for (final ItemStack is : questData.blocksPlaced) {
if (event.getBlock().getType().equals(is.getType()) && is.getAmount() > 0) {
ItemStack toPlace = new ItemStack(is.getType(), 64);
ItemStack goal = new ItemStack(is.getType(), 64);
for (final ItemStack stack : currentStage.getBlocksToPlace()) {
if (BukkitItemUtil.compareItems(is, stack, true) == 0) {
toPlace = stack;
goal = stack;
}
}
final BukkitQuesterPreUpdateObjectiveEvent preEvent
= new BukkitQuesterPreUpdateObjectiveEvent(quester, quest,
new BukkitObjective(placeType, null, is.getAmount(), toPlace.getAmount()));
new BukkitObjective(placeType, null, is.getAmount(), goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(preEvent);
final int index = questData.blocksPlaced.indexOf(is);
@ -122,7 +123,7 @@ public class BukkitBlockListener implements Listener {
final BukkitQuesterPostUpdateObjectiveEvent postEvent
= new BukkitQuesterPostUpdateObjectiveEvent(quester, quest,
new BukkitObjective(placeType, null, newAmount, toPlace.getAmount()));
new BukkitObjective(placeType, null, newAmount, goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(postEvent);
}
}
@ -134,16 +135,16 @@ public class BukkitBlockListener implements Listener {
final BukkitQuestProgress qQuestData = (BukkitQuestProgress) q.getQuestDataOrDefault(cq);
for (final ItemStack is : qQuestData.blocksPlaced) {
if (event.getBlock().getType().equals(is.getType()) && is.getAmount() > 0) {
ItemStack toPlace = new ItemStack(is.getType(), 64);
ItemStack goal = new ItemStack(is.getType(), 64);
for (final ItemStack stack : ((BukkitStage) quester.getCurrentStage(cq)).getBlocksToPlace()) {
if (BukkitItemUtil.compareItems(is, stack, true) == 0) {
toPlace = stack;
goal = stack;
}
}
final BukkitQuesterPreUpdateObjectiveEvent preEvent
= new BukkitQuesterPreUpdateObjectiveEvent((BukkitQuester) q, cq,
new BukkitObjective(placeType, null, is.getAmount(), toPlace.getAmount()));
new BukkitObjective(placeType, null, is.getAmount(), goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(preEvent);
final int index = qQuestData.blocksPlaced.indexOf(is);
@ -153,7 +154,7 @@ public class BukkitBlockListener implements Listener {
final BukkitQuesterPostUpdateObjectiveEvent postEvent
= new BukkitQuesterPostUpdateObjectiveEvent((BukkitQuester) q, cq,
new BukkitObjective(placeType, null, newAmount, toPlace.getAmount()));
new BukkitObjective(placeType, null, newAmount, goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(postEvent);
}
}
@ -249,28 +250,30 @@ public class BukkitBlockListener implements Listener {
if (quest.getOptions().canIgnoreBlockReplace()) {
// Ignore blocks replaced once broken (self)
if (currentStage.containsObjective(breakType)) {
for (final ItemStack is : questData.blocksBroken) {
for (int i = 0; i < questData.blocksBroken.size(); i++) {
final int progress = questData.blocksBroken.get(i) - 1;
if (progress < 0) {
break;
}
final ItemStack is = currentStage.getBlocksToBreak().get(i);
if (event.getBlock().getType().equals(is.getType()) && is.getAmount() > 0) {
ItemStack toBreak = new ItemStack(is.getType(), 64);
ItemStack goal = new ItemStack(is.getType(), 64);
for (final ItemStack stack : currentStage.getBlocksToBreak()) {
if (BukkitItemUtil.compareItems(is, stack, true) == 0) {
toBreak = stack;
goal = stack;
}
}
final BukkitQuesterPreUpdateObjectiveEvent preEvent
= new BukkitQuesterPreUpdateObjectiveEvent(quester, quest,
new BukkitObjective(placeType, null, is.getAmount(), toBreak.getAmount()));
new BukkitObjective(placeType, null, is.getAmount(), goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(preEvent);
final int index = questData.blocksBroken.indexOf(is);
final int newAmount = is.getAmount() - 1;
is.setAmount(newAmount);
questData.blocksBroken.set(index, is);
questData.blocksBroken.set(i, progress);
final BukkitQuesterPostUpdateObjectiveEvent postEvent
= new BukkitQuesterPostUpdateObjectiveEvent(quester, quest,
new BukkitObjective(placeType, null, newAmount, toBreak.getAmount()));
new BukkitObjective(placeType, null, progress, goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(postEvent);
}
}
@ -280,28 +283,30 @@ public class BukkitBlockListener implements Listener {
(final Quester q, final Quest cq) -> {
if (!dispatchedBreakQuestIDs.contains(cq.getId())) {
final BukkitQuestProgress qQuestData = (BukkitQuestProgress) q.getQuestDataOrDefault(cq);
for (final ItemStack is : qQuestData.blocksBroken) {
for (final int i : new LinkedList<>(qQuestData.blocksBroken)) {
final int progress = qQuestData.blocksBroken.get(i) - 1;
if (progress < 0) {
break;
}
final ItemStack is = currentStage.getBlocksToBreak().get(i);
if (event.getBlock().getType().equals(is.getType()) && is.getAmount() > 0) {
ItemStack toBreak = new ItemStack(is.getType(), 64);
ItemStack goal = new ItemStack(is.getType(), 64);
for (final ItemStack stack : ((BukkitStage) quester.getCurrentStage(cq)).getBlocksToBreak()) {
if (BukkitItemUtil.compareItems(is, stack, true) == 0) {
toBreak = stack;
goal = stack;
}
}
final BukkitQuesterPreUpdateObjectiveEvent preEvent
= new BukkitQuesterPreUpdateObjectiveEvent((BukkitQuester) q, cq,
new BukkitObjective(breakType, null, is.getAmount(), toBreak.getAmount()));
new BukkitObjective(breakType, null, is.getAmount(), goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(preEvent);
final int index = qQuestData.blocksBroken.indexOf(is);
final int newAmount = is.getAmount() - 1;
is.setAmount(newAmount);
qQuestData.blocksBroken.set(index, is);
qQuestData.blocksBroken.set(i, progress);
final BukkitQuesterPostUpdateObjectiveEvent postEvent
= new BukkitQuesterPostUpdateObjectiveEvent((BukkitQuester) q, cq,
new BukkitObjective(breakType, null, newAmount, toBreak.getAmount()));
new BukkitObjective(breakType, null, progress, goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(postEvent);
}
}

View File

@ -25,33 +25,40 @@ public class BukkitQuestProgress implements QuestProgress {
public BukkitQuestProgress(final Quester quester) {
this.quester = quester;
}
public LinkedList<ItemStack> blocksBroken = new LinkedList<ItemStack>() {
// TODO increase serial id
public LinkedList<Integer> blocksBroken = new LinkedList<Integer>() {
private static final long serialVersionUID = -6071822509475270168L;
private static final long serialVersionUID = -6071822509475270169L;
@Override
public ItemStack set(final int index, final ItemStack key) {
final ItemStack data = super.set(index, key);
public boolean add(final Integer e) {
final boolean b = super.add(e);
if (doJournalUpdate)
quester.updateJournal();
return data;
return b;
}
@Override
public boolean add(final ItemStack key) {
final boolean data = super.add(key);
public void add(final int index, final Integer element) {
super.add(index, element);
if (doJournalUpdate)
quester.updateJournal();
return data;
}
@Override
public boolean remove(final Object key) {
final boolean i = super.remove(key);
public boolean addAll(final Collection<? extends Integer> c) {
final boolean b = super.addAll(c);
if (doJournalUpdate)
quester.updateJournal();
return i;
return b;
}
@Override
public boolean addAll(final int index, final Collection<? extends Integer> c) {
final boolean b = super.addAll(index, c);
if (doJournalUpdate)
quester.updateJournal();
return b;
}
@Override
@ -62,11 +69,27 @@ public class BukkitQuestProgress implements QuestProgress {
}
@Override
public boolean addAll(final Collection<? extends ItemStack> m) {
final boolean i = super.addAll(m);
public boolean remove(final Object o) {
final boolean b = super.remove(o);
if (doJournalUpdate)
quester.updateJournal();
return i;
return b;
}
@Override
public Integer remove(final int index) {
final Integer s = super.remove(index);
if (doJournalUpdate)
quester.updateJournal();
return s;
}
@Override
public Integer set(final int index, final Integer element) {
final Integer s = super.set(index, element);
if (doJournalUpdate)
quester.updateJournal();
return s;
}
};
@ -1057,11 +1080,11 @@ public class BukkitQuestProgress implements QuestProgress {
private boolean doJournalUpdate = true;
@Override
public LinkedList<ItemStack> getBlocksBroken() {
public LinkedList<Integer> getBlocksBroken() {
return blocksBroken;
}
public void setBlocksBroken(final LinkedList<ItemStack> blocksBroken) {
public void setBlocksBroken(final LinkedList<Integer> blocksBroken) {
this.blocksBroken = blocksBroken;
if (doJournalUpdate) {
quester.updateJournal();

View File

@ -1187,18 +1187,16 @@ public class BukkitQuester implements Quester {
}
final BukkitQuestProgress data = (BukkitQuestProgress) getQuestDataOrDefault(quest);
final LinkedList<Objective> objectives = new LinkedList<>();
for (final ItemStack goal : stage.getBlocksToBreak()) {
for (final ItemStack progress : data.blocksBroken) {
if (progress.getType().equals(goal.getType()) && progress.getDurability() == goal.getDurability()) {
final ChatColor color = progress.getAmount() < goal.getAmount() ? ChatColor.GREEN : ChatColor.GRAY;
String message = formatCurrentObjectiveMessage(color, BukkitLang.get(getPlayer(), "break"),
progress.getAmount(), goal.getAmount());
if (formatNames) {
message = message.replace("<item>", BukkitItemUtil.getName(progress));
}
objectives.add(new BukkitObjective(ObjectiveType.BREAK_BLOCK, message, progress, goal));
}
for (int i = 0; i < data.getBlocksBroken().size(); i++) {
final int progress = data.getBlocksBroken().get(i);
final ItemStack goal = stage.getBlocksToBreak().get(i);
final ChatColor color = progress < goal.getAmount() ? ChatColor.GREEN : ChatColor.GRAY;
String message = formatCurrentObjectiveMessage(color, BukkitLang.get(getPlayer(), "break"),
progress, goal.getAmount());
if (formatNames) {
message = message.replace("<item>", BukkitItemUtil.getName(goal));
}
objectives.add(new BukkitObjective(ObjectiveType.BREAK_BLOCK, message, progress, goal));
}
for (final ItemStack goal : stage.getBlocksToDamage()) {
for (final ItemStack progress : data.blocksDamaged) {
@ -1800,107 +1798,79 @@ public class BukkitQuester implements Quester {
* Marks block as broken if Quester has such an objective
*
* @param quest The quest for which the block is being broken
* @param itemStack The block being broken
* @param broken The block being broken
*/
@SuppressWarnings("deprecation")
public void breakBlock(final Quest quest, final ItemStack itemStack) {
itemStack.setAmount(0);
ItemStack broken = itemStack;
ItemStack toBreak = itemStack;
public void breakBlock(final Quest quest, final ItemStack broken) {
ItemStack goal = null;
final BukkitQuestProgress bukkitQuestData = (BukkitQuestProgress) getQuestDataOrDefault(quest);
for (final ItemStack is : bukkitQuestData.blocksBroken) {
if (itemStack.getType() == is.getType()) {
if (itemStack.getType().isSolid() && is.getType().isSolid()) {
for (final ItemStack toBreak : ((BukkitStage) getCurrentStage(quest)).getBlocksToBreak()) {
if (goal != null) {
break;
}
if (broken.getType() == toBreak.getType()) {
if (broken.getType().isSolid() && toBreak.getType().isSolid()) {
// Blocks are solid so check for durability
if (itemStack.getDurability() == is.getDurability()) {
broken = is;
if (broken.getDurability() == toBreak.getDurability()) {
goal = toBreak;
} else if (!plugin.getLocaleManager().isBelow113()) {
// Ignore durability for 1.13+
broken = is;
goal = toBreak;
}
} else if (itemStack.getData() instanceof Crops && is.getData() instanceof Crops) {
if (is.getDurability() > 0) {
// Age is specified so check for durability
if (itemStack.getDurability() == is.getDurability()) {
broken = is;
} else if (broken.getData() instanceof Crops && toBreak.getData() instanceof Crops) {
if (toBreak.getDurability() > 0) {
// Age toBreak specified so check for durability
if (broken.getDurability() == toBreak.getDurability()) {
goal = toBreak;
}
} else {
// Age is unspecified so ignore durability
broken = is;
// Age toBreak unspecified so ignore durability
goal = toBreak;
}
} else if (itemStack.getType().name().equals("RED_ROSE")) {
} else if (broken.getType().name().equals("RED_ROSE")) {
// Flowers are unique so check for durability
if (itemStack.getDurability() == is.getDurability()) {
broken = is;
if (broken.getDurability() == toBreak.getDurability()) {
goal = toBreak;
}
} else {
// Blocks are not solid so ignore durability
broken = is;
goal = toBreak;
}
}
}
for (final ItemStack is : ((BukkitStage) getCurrentStage(quest)).getBlocksToBreak()) {
if (itemStack.getType() == is.getType()) {
if (itemStack.getType().isSolid() && is.getType().isSolid()) {
// Blocks are solid so check for durability
if (itemStack.getDurability() == is.getDurability()) {
toBreak = is;
} else if (!plugin.getLocaleManager().isBelow113()) {
// Ignore durability for 1.13+
toBreak = is;
}
} else if (itemStack.getData() instanceof Crops && is.getData() instanceof Crops) {
if (is.getDurability() > 0) {
// Age is specified so check for durability
if (itemStack.getDurability() == is.getDurability()) {
toBreak = is;
}
} else {
// Age is unspecified so ignore durability
toBreak = is;
}
} else if (itemStack.getType().name().equals("RED_ROSE")) {
// Flowers are unique so check for durability
if (itemStack.getDurability() == is.getDurability()) {
toBreak = is;
}
} else {
// Blocks are not solid so ignore durability
toBreak = is;
}
}
if (goal == null) {
// No match found
return;
}
final ObjectiveType type = ObjectiveType.BREAK_BLOCK;
final BukkitQuesterPreUpdateObjectiveEvent preEvent = new BukkitQuesterPreUpdateObjectiveEvent(this, quest,
new BukkitObjective(type, null, broken.getAmount(), toBreak.getAmount()));
new BukkitObjective(type, null, broken.getAmount(), goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(preEvent);
final ItemStack newBroken = broken;
if (broken.getAmount() < toBreak.getAmount()) {
newBroken.setAmount(broken.getAmount() + 1);
if (bukkitQuestData.blocksBroken.contains(broken)) {
bukkitQuestData.blocksBroken.set(bukkitQuestData.blocksBroken.indexOf(broken), newBroken);
if (broken.getAmount() == toBreak.getAmount()) {
finishObjective(quest, new BukkitObjective(type, null, itemStack, toBreak), null, null, null, null, null, null, null);
// Multiplayer
final ItemStack finalBroken = broken;
final ItemStack finalToBreak = toBreak;
dispatchMultiplayerObjectives(quest, getCurrentStage(quest), (final Quester q) -> {
((BukkitQuestProgress) q.getQuestDataOrDefault(quest)).blocksBroken.set(bukkitQuestData.blocksBroken
.indexOf(finalBroken), newBroken);
q.finishObjective(quest, new BukkitObjective(type, null, itemStack, finalToBreak), null, null, null, null, null,
null, null);
return null;
});
}
}
final int breakIndex = getCurrentStage(quest).getBlocksToBreak().indexOf(goal);
if (bukkitQuestData.blocksBroken.get(breakIndex) > goal.getAmount()) {
return;
}
final int progress = bukkitQuestData.blocksBroken.get(breakIndex) + 1;
bukkitQuestData.blocksBroken.set(breakIndex, progress);
if (progress >= goal.getAmount()) {
finishObjective(quest, new BukkitObjective(type, null, progress, goal), null, null, null,
null, null, null, null);
// Multiplayer
final ItemStack finalGoal = goal;
dispatchMultiplayerObjectives(quest, getCurrentStage(quest), (final Quester q) -> {
((BukkitQuestProgress) q.getQuestDataOrDefault(quest)).blocksBroken.set(breakIndex, progress);
q.finishObjective(quest, new BukkitObjective(type, null, progress, finalGoal), null, null, null,
null, null, null, null);
return null;
});
}
final BukkitQuesterPostUpdateObjectiveEvent postEvent = new BukkitQuesterPostUpdateObjectiveEvent(this, quest,
new BukkitObjective(type, null, newBroken.getAmount(), toBreak.getAmount()));
new BukkitObjective(type, null, progress, goal.getAmount()));
plugin.getServer().getPluginManager().callEvent(postEvent);
}
@ -3649,13 +3619,8 @@ public class BukkitQuester implements Quester {
}
final BukkitStage bukkitStage = (BukkitStage) quest.getStage(stage);
if (!bukkitStage.getBlocksToBreak().isEmpty()) {
for (final ItemStack toBreak : bukkitStage.getBlocksToBreak()) {
final ItemStack temp = getLowItemStack(toBreak.getType(), toBreak.getDurability());
if (data.blocksBroken.contains(toBreak)) {
data.blocksBroken.set(data.blocksBroken.indexOf(temp), temp);
} else {
data.blocksBroken.add(temp);
}
for (final ItemStack ignored : bukkitStage.getBlocksToBreak()) {
data.blocksBroken.add(0);
}
}
if (!bukkitStage.getBlocksToDamage().isEmpty()) {
@ -3860,11 +3825,7 @@ public class BukkitQuester implements Quester {
continue;
}
if (!questData.blocksBroken.isEmpty()) {
final LinkedList<Integer> blockAmounts = new LinkedList<>();
for (final ItemStack m : questData.blocksBroken) {
blockAmounts.add(m.getAmount());
}
questSec.set("blocks-broken-amounts", blockAmounts);
questSec.set("blocks-broken-amounts", questData.blocksBroken);
}
if (!questData.blocksDamaged.isEmpty()) {
final LinkedList<Integer> blockAmounts = new LinkedList<>();

View File

@ -44,13 +44,17 @@ public class BukkitObjective implements Objective {
this.progress = ((ItemStack) progressObj).getAmount();
} else if (progressObj instanceof BukkitCountableMob) {
this.progress = ((BukkitCountableMob) progressObj).getCount();
} else if (progressObj instanceof Integer) {
this.progress = (int) progress;
} else {
this.progress = 0;
}
if (goalObj instanceof ItemStack) {
this.goal = ((ItemStack) goalObj).getAmount();
} else if (goalObj instanceof BukkitCountableMob) {
} else if (goalObj instanceof BukkitCountableMob) {
this.goal = ((BukkitCountableMob) goalObj).getCount();
} else if (goalObj instanceof Integer) {
this.goal = (int) goalObj;
} else {
this.goal = 0;
}

View File

@ -176,15 +176,8 @@ public class BukkitQuesterYamlStorage implements QuesterStorageImpl {
final BukkitQuestProgress bukkitQuestData = (BukkitQuestProgress) quester.getQuestDataOrDefault(quest);
if (questSec.contains("blocks-broken-amounts")) {
final List<Integer> brokenAmounts = questSec.getIntegerList("blocks-broken-amounts");
int index = 0;
for (final int amt : brokenAmounts) {
final ItemStack is = stage.getBlocksToBreak().get(index);
final ItemStack temp = is.clone();
temp.setAmount(amt);
if (bukkitQuestData.getBlocksBroken().size() > 0) {
bukkitQuestData.blocksBroken.set(index, temp);
}
index++;
for (int i = 0; i < brokenAmounts.size(); i++) {
bukkitQuestData.blocksBroken.set(i, brokenAmounts.get(i));
}
}
if (questSec.contains("blocks-damaged-amounts")) {

View File

@ -360,7 +360,7 @@ public class BukkitQuesterSqlStorage implements QuesterStorageImpl {
try (final PreparedStatement ps = c.prepareStatement(statementProcessor.apply(PLAYER_QUEST_PROGRESS_INSERT))) {
ps.setString(1, uniqueId.toString());
ps.setString(2, entry.getKey().getId());
ps.setString(3, serializeItemStackProgress(entry.getValue().getBlocksBroken()));
ps.setString(3, serializeProgress(entry.getValue().getBlocksBroken()));
ps.setString(4, serializeItemStackProgress(entry.getValue().getBlocksDamaged()));
ps.setString(5, serializeItemStackProgress(entry.getValue().getBlocksPlaced()));
ps.setString(6, serializeItemStackProgress(entry.getValue().getBlocksUsed()));
@ -462,8 +462,7 @@ public class BukkitQuesterSqlStorage implements QuesterStorageImpl {
final BukkitQuestProgress data = new BukkitQuestProgress(quester);
if (quest != null && quester.getCurrentStage(quest) != null) {
final BukkitStage stage = (BukkitStage) quester.getCurrentStage(quest);
data.blocksBroken.addAll(deserializeItemStackProgress(rs.getString("blocks_broken"),
stage.getBlocksToBreak()));
data.blocksBroken.addAll(deserializeIntProgress(rs.getString("blocks_broken")));
data.blocksDamaged.addAll(deserializeItemStackProgress(rs.getString("blocks_damaged"),
stage.getBlocksToDamage()));
data.blocksPlaced.addAll(deserializeItemStackProgress(rs.getString("blocks_placed"),
@ -623,6 +622,9 @@ public class BukkitQuesterSqlStorage implements QuesterStorageImpl {
}
}
/**
* @deprecated Legacy code, do not use. Will be removed in a later version.
*/
@SuppressWarnings("deprecation")
public LinkedList<ItemStack> deserializeItemStackProgress(String string, final LinkedList<ItemStack> objective) {
final LinkedList<ItemStack> list = new LinkedList<>();