Skip to content

Commit

Permalink
Continuous translation upon item pickup is now handled by the SPoiler…
Browse files Browse the repository at this point in the history
…Alert plugin to avoid conflicts with te BlockTyperPlugin overwriting the expiration date when item's language is translated.
  • Loading branch information
BuildTools committed Jan 19, 2017
1 parent c650709 commit 309c073
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SpoilerAlertPlugin extends BlockTyperPlugin {

public SpoilerAlertPlugin() {
super();
useOnPickupTranslationListener = false;
}

public void onEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
if (daysSourceExpired == null || daysSourceExpired < 1) {
String cakeNotExpiredText = plugin.getLocalizedMessage(LocalizedMessageEnum.CAKE_NOT_EXPIRED.getKey(), event.getPlayer());
if (!isEating)
event.getPlayer().sendMessage(cakeNotExpiredText + " [" + expirationDate.getDateString(event.getPlayer(), plugin) + "].");
event.getPlayer().sendMessage(cakeNotExpiredText + " [" + expirationDate.getDateString(event.getPlayer(), spoilerAlertPlugin) + "].");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;

import com.blocktyper.spoileralert.SpoilerAlertPlugin;

Expand All @@ -13,7 +14,7 @@ public PickupListener(SpoilerAlertPlugin plugin) {
super(plugin);
// TODO Auto-generated constructor stub
}

/*
* ON PLAYER PICK UP
*/
Expand All @@ -26,7 +27,13 @@ public void onPlayerPickupItem(PlayerPickupItemEvent event) {
return;
}

item.setItemStack(setExpirationDate(item.getItemStack(), item.getWorld(), null, event.getPlayer()));
ItemStack newItemStack = setExpirationDate(item.getItemStack(), item.getWorld(), null, event.getPlayer());

if (continuousTranslationEnabled()) {
newItemStack = convertItemStackLanguage(newItemStack, event.getPlayer());
}

item.setItemStack(newItemStack);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,40 @@
import org.bukkit.block.Block;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import com.blocktyper.helpers.InvisibleLoreHelper;
import com.blocktyper.nbt.NBTItem;
import com.blocktyper.recipes.translation.ContinuousTranslationListener;
import com.blocktyper.spoileralert.ConfigKeyEnum;
import com.blocktyper.spoileralert.LocalizedMessageEnum;
import com.blocktyper.spoileralert.PerishableBlock;
import com.blocktyper.spoileralert.PerishableBlockRepo;
import com.blocktyper.spoileralert.SpoilerAlertCalendar;
import com.blocktyper.spoileralert.SpoilerAlertPlugin;

public abstract class SpoilerAlertListenerBase implements Listener {
public abstract class SpoilerAlertListenerBase extends ContinuousTranslationListener {

public static final int DEFAULT_LIFE_SPAN_IN_DAYS = 7;
public static final String DATA_KEY_SPOILER_ALERT_PERISHABLE_BLOCKS = "DATA_KEY_SPOILER_ALERT_PERISHABLE_BLOCKS";

public static final String NBT_SPOILER_ALERT_EXPIRATION_DATE = "SPOILER_ALERT_EXPIRATION_DATE";
public static final String INVISIBLE_PREFIX_SPOILER_ALERT_EXPIRATION_DATE = "SP_EXP#";

public static final String NBT_SPOILER_ALERT_DATE_TYPE = "NBT_SPOILER_ALERT_DATE_TYPE";
public static final String NBT_VALUE_SPOILER_ALERT_REAL_DATE_TYPE = "REAL";
public static final String NBT_VALUE_SPOILER_ALERT_FAKE_DATE_TYPE = "FAKE";

protected static PerishableBlockRepo perishableBlockRepo;

protected SpoilerAlertPlugin plugin;
protected SpoilerAlertPlugin spoilerAlertPlugin;

public SpoilerAlertListenerBase(SpoilerAlertPlugin plugin) {
this.plugin = plugin;
super(plugin);
this.spoilerAlertPlugin = plugin;
this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
initPerishableBlockRepo();
}
Expand Down Expand Up @@ -87,13 +88,13 @@ protected ItemStack setExpirationDate(ItemStack itemStack, World world, Long day
}

List<String> lore = itemMeta.getLore();

String expectedDateType = NBT_VALUE_SPOILER_ALERT_FAKE_DATE_TYPE;

if (plugin.getConfig().getBoolean(ConfigKeyEnum.USE_REAL_DATES.getKey(), false)) {
expectedDateType = NBT_VALUE_SPOILER_ALERT_REAL_DATE_TYPE;
}

String dateType = nbtItemForExistingCheck.getString(NBT_SPOILER_ALERT_DATE_TYPE);

if (!nbtItemForExistingCheck.hasKey(NBT_SPOILER_ALERT_EXPIRATION_DATE) || !expectedDateType.equals(dateType)) {
Expand Down Expand Up @@ -128,7 +129,8 @@ protected ItemStack setExpirationDate(ItemStack itemStack, World world, Long day
Date existingExpirationDate = getDateFromNbtString(expirationDateNbtString);
String formattedExpirationDate = getStringfromDate(existingExpirationDate, player);
daysExpired = getDaysExpired(expirationDateNbtString, world);
lore.add(getExpirationDateLoreLine(player, (daysExpired == null || daysExpired < 1 ? "" : ChatColor.RED) + formattedExpirationDate));
lore.add(getExpirationDateLoreLine(player,
(daysExpired == null || daysExpired < 1 ? "" : ChatColor.RED) + formattedExpirationDate));
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
} catch (ParseException e) {
Expand Down Expand Up @@ -171,19 +173,19 @@ private ItemStack getItemWithNbtTagExpirationDate(HumanEntity player, ItemStack
SpoilerAlertCalendar expirationDate = new SpoilerAlertCalendar(player.getWorld());
expirationDate.addDays(days);
expDateAsNbtString = expirationDate.getNbtDateString();
formattedDateString = expirationDate.getDateString(player, plugin);
formattedDateString = expirationDate.getDateString(player, spoilerAlertPlugin);
}

if (lore == null)
lore = new ArrayList<>();

lore = lore.stream().filter(l -> !loreLineContainsInvisExpirationDatePrefix(l))
.collect(Collectors.toList());


lore = lore.stream().filter(l -> !loreLineContainsInvisExpirationDatePrefix(l)).collect(Collectors.toList());

if (lore == null)
lore = new ArrayList<>();

lore.add(getExpirationDateLoreLine(player, (daysExpired == null || daysExpired < 1 ? "" : ChatColor.RED) + formattedDateString));
lore.add(getExpirationDateLoreLine(player,
(daysExpired == null || daysExpired < 1 ? "" : ChatColor.RED) + formattedDateString));
itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);

Expand Down Expand Up @@ -271,7 +273,7 @@ private String getNbtStringfromDate(Date date) {
}

private String getStringfromDate(Date date, HumanEntity player) {
String dateFormat = plugin.getPlayerDateFormat(player);
String dateFormat = spoilerAlertPlugin.getPlayerDateFormat(player);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String dateString = sdf.format(date);
return dateString;
Expand Down

0 comments on commit 309c073

Please sign in to comment.