use of org.spongepowered.api.block.BlockState in project RedProtect by FabioZumbi12.
the class RPPlayerListener method onInteractBlock.
// listen all
@Listener(order = Order.FIRST, beforeModifications = true)
public void onInteractBlock(InteractBlockEvent event, @First Player p) {
RedProtect.get().logger.debug("player", "RPPlayerListener - InteractBlockEvent canceled? " + event.isCancelled());
BlockSnapshot b = event.getTargetBlock();
BlockState bstate = b.getState();
Location<World> l = null;
if (!b.getState().getType().equals(BlockTypes.AIR)) {
l = b.getLocation().get();
RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent event. The block is " + bstate.getType().getName());
} else {
l = p.getLocation();
}
Region r = RedProtect.get().rm.getTopRegion(l);
ItemType itemInHand = ItemTypes.NONE;
ItemStack stack = ItemStack.of(ItemTypes.NONE, 1);
if (p.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
stack = p.getItemInHand(HandTypes.MAIN_HAND).get();
itemInHand = stack.getItem();
if (RPUtil.removeGuiItem(stack)) {
p.setItemInHand(HandTypes.MAIN_HAND, ItemStack.of(ItemTypes.NONE, 1));
event.setCancelled(true);
}
} else if (p.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
stack = p.getItemInHand(HandTypes.OFF_HAND).get();
itemInHand = stack.getItem();
if (RPUtil.removeGuiItem(stack)) {
p.setItemInHand(HandTypes.OFF_HAND, ItemStack.of(ItemTypes.NONE, 1));
event.setCancelled(true);
}
}
if (itemInHand.getId().equalsIgnoreCase(RedProtect.get().cfgs.getString("wands.infoWandID"))) {
r = RedProtect.get().rm.getTopRegion(l);
if (RedProtect.get().ph.hasUserPerm(p, "RedProtect.get().infowand")) {
if (r == null) {
RPLang.sendMessage(p, "playerlistener.noregion.atblock");
} else if (r.canBuild(p)) {
p.sendMessage(RPUtil.toText(RPLang.get("general.color") + "--------------- [&e" + r.getName() + RPLang.get("general.color") + "] ---------------"));
p.sendMessage(r.info());
p.sendMessage(RPUtil.toText(RPLang.get("general.color") + "-----------------------------------------"));
} else {
p.sendMessage(RPUtil.toText(RPLang.get("playerlistener.region.entered").replace("{region}", r.getName()).replace("{leaders}", RPUtil.UUIDtoPlayer(r.getLeadersDesc()))));
}
event.setCancelled(true);
return;
}
}
// start player checks
if (r == null) {
if (b != null && (bstate instanceof Container || RedProtect.get().cfgs.getStringList("private.allowed-blocks").contains(bstate.getType().getName()))) {
Boolean out = RedProtect.get().cfgs.getBool("private.allow-outside");
if (out && !cont.canOpen(b, p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantopen");
event.setCancelled(true);
} else {
int x = b.getLocation().get().getBlockX();
int y = b.getLocation().get().getBlockY();
int z = b.getLocation().get().getBlockZ();
RPLang.sendMessage(p, RPLang.get("playerlistener.region.opened").replace("{region}", "X:" + x + " Y:" + y + " Z:" + z));
}
}
}
} else {
// if (r != null) && (b != null) >>
if (b != null) {
if (bstate.getType().equals(BlockTypes.DRAGON_EGG) || bstate.getType().equals(BlockTypes.BED) || bstate.getType().equals(BlockTypes.NOTEBLOCK) || bstate.getType().getName().contains("repeater") || bstate.getType().getName().contains("comparator")) {
if (!r.canBuild(p)) {
RPLang.sendMessage(p, "playerlistener.region.cantinteract");
event.setCancelled(true);
}
} else if ((b.getState() instanceof Carrier) || RedProtect.get().cfgs.getStringList("private.allowed-blocks").contains(bstate.getType().getName())) {
if ((r.canChest(p) && !cont.canOpen(b, p) || (!r.canChest(p) && cont.canOpen(b, p)) || (!r.canChest(p) && !cont.canOpen(b, p)))) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantopen");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.opened").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (bstate.getType().getName().contains("lever")) {
if (!r.canLever(p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantlever");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.levertoggled").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (bstate.getType().getName().contains("button")) {
if (!r.canButton(p)) {
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantbutton");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.buttonactivated").replace("{region}", RPUtil.UUIDtoPlayer(r.getLeadersDesc())));
}
}
} else if (RPDoor.isOpenable(b)) {
if (!r.canDoor(p)) /* || (r.canDoor(p) && !cont.canOpen(b, p))*/
{
if (!RedProtect.get().ph.hasPerm(p, "RedProtect.get().bypass")) {
RPLang.sendMessage(p, "playerlistener.region.cantdoor");
event.setCancelled(true);
} else {
RPLang.sendMessage(p, "playerlistener.region.opendoor");
RPDoor.ChangeDoor(b, r);
}
} else {
RPDoor.ChangeDoor(b, r);
}
} else if (bstate.getType().getName().contains("rail")) {
if (!r.canMinecart(p)) {
RPLang.sendMessage(p, "blocklistener.region.cantplace");
event.setCancelled(true);
}
} else if (bstate.getType().getName().contains("sign") && !r.canSign(p)) {
if (b.get(Keys.SIGN_LINES).isPresent()) {
List<Text> sign = b.get(Keys.SIGN_LINES).get();
for (String tag : RedProtect.get().cfgs.getStringList("region-settings.allow-sign-interact-tags")) {
// check first rule
if (tag.equalsIgnoreCase(sign.get(0).toPlain())) {
return;
}
// check if tag is leaders or members names
if (tag.equalsIgnoreCase("{membername}")) {
for (String leader : r.getLeaders()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(leader))) {
return;
}
}
for (String member : r.getMembers()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(member))) {
return;
}
}
for (String admin : r.getAdmins()) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(admin))) {
return;
}
}
}
// check if tag is player name
if (tag.equalsIgnoreCase("{playername}")) {
if (sign.get(0).toPlain().equalsIgnoreCase(RPUtil.UUIDtoPlayer(p.getName()))) {
return;
}
}
}
}
RPLang.sendMessage(p, "playerlistener.region.cantinteract.signs");
event.setCancelled(true);
} else if ((itemInHand.equals(ItemTypes.FLINT_AND_STEEL) || itemInHand.equals(ItemTypes.WATER_BUCKET) || itemInHand.equals(ItemTypes.BUCKET) || itemInHand.equals(ItemTypes.LAVA_BUCKET) || itemInHand.equals(ItemTypes.ITEM_FRAME) || itemInHand.equals(ItemTypes.PAINTING)) && !r.canBuild(p)) {
RPLang.sendMessage(p, RPLang.get("playerlistener.region.cantuse"));
event.setCancelled(true);
} else if (!r.allowMod(p) && !RPUtil.isBukkitBlock(bstate)) {
RPLang.sendMessage(p, "playerlistener.region.cantinteract");
event.setCancelled(true);
}
}
}
}
use of org.spongepowered.api.block.BlockState in project TotalEconomy by Erigitic.
the class JobManager method onPlayerPlaceBlock.
/**
* Used for the place option in jobs. Will check if the job has the place node and if it does it will check if the
* block that was placed is present in the config of the player's job. If it is, it will grab the job exp reward as
* well as the pay.
*
* @param event ChangeBlockEvent.Place
*/
@Listener
public void onPlayerPlaceBlock(ChangeBlockEvent.Place event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = event.getCause().first(Player.class).get();
UUID playerUUID = player.getUniqueId();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
BlockState state = event.getTransactions().get(0).getFinal().getState();
String blockName = state.getType().getName();
// Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
if (accountManager.getUserOption("totaleconomy:block-place-info", player).orElse("0").equals("1")) {
List<BlockTrait<?>> traits = new ArrayList<>(state.getTraits());
int count = traits.size();
List<Text> traitTexts = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Object traitValue = state.getTraitValue(traits.get(i)).orElse(null);
traitTexts.add(i, Text.of(traits.get(i).getName(), '=', traitValue != null ? traitValue.toString() : "null"));
}
Text t = Text.of(TextColors.GRAY, "TRAITS:\n ", Text.joinWith(Text.of(",\n "), traitTexts.toArray(new Text[traits.size()])));
player.sendMessage(Text.of("Block-Name: ", blockName));
player.sendMessage(t);
}
if (optPlayerJob.isPresent()) {
Optional<TEActionReward> reward = Optional.empty();
List<String> sets = optPlayerJob.get().getSets();
for (String s : sets) {
Optional<TEJobSet> optSet = getJobSet(s);
if (!optSet.isPresent()) {
logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
continue;
}
Optional<TEAction> action = optSet.get().getActionFor("place", blockName);
if (!action.isPresent()) {
continue;
}
Optional<TEActionReward> currentReward = action.get().evaluatePlace(logger, state);
if (!reward.isPresent()) {
reward = currentReward;
continue;
}
if (!currentReward.isPresent()) {
continue;
}
// Use the one giving higher exp in case of duplicates
if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
reward = currentReward;
}
}
if (reward.isPresent()) {
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
boolean notify = getNotificationState(playerUUID);
int expAmount = reward.get().getExpReward();
BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
Currency currency = totalEconomy.getDefaultCurrency();
if (reward.get().getCurrencyId() != null) {
Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
if (currencyOpt.isPresent()) {
currency = currencyOpt.get();
}
}
if (notify) {
notifyPlayer(player, payAmount, currency);
}
addExp(player, expAmount);
playerAccount.deposit(currency, payAmount, event.getCause());
checkForLevel(player);
}
}
}
}
use of org.spongepowered.api.block.BlockState in project core by CubeEngine.
the class MaterialMatcher method buildVariantMap.
private Map<BlockType, Map<String, BlockState>> buildVariantMap() {
Map<BlockType, Map<String, BlockState>> blockStateItemsByType = new HashMap<>();
for (Entry<String, BlockState> entry : blockStateItems.entrySet()) {
BlockType itemType = entry.getValue().getType();
Map<String, BlockState> itemTypes = blockStateItemsByType.computeIfAbsent(itemType, k -> new HashMap<>());
itemTypes.put(entry.getKey(), entry.getValue());
}
Map<BlockType, Map<String, BlockState>> variants = new HashMap<>();
blockStateItemsByType.entrySet().stream().filter(e -> e.getValue().size() != 1).forEach(e -> {
Map<String, Set<String>> variantNames = new HashMap<>();
Map<List<String>, BlockState> fullVariant = new HashMap<>();
for (Entry<String, BlockState> entry : e.getValue().entrySet()) {
String variant = entry.getKey();
variant = variant.substring(variant.indexOf("[") + 1, variant.indexOf("]"));
// multiple variants
String[] split = variant.split(",");
fullVariant.put(Arrays.asList(split), entry.getValue());
for (String variantEntry : split) {
String[] variantEntryPart = variantEntry.split("=");
Set<String> variantValues = variantNames.computeIfAbsent(variantEntryPart[0], k -> new HashSet<>());
variantValues.add(variantEntryPart[1]);
}
}
for (Entry<String, Set<String>> entry : variantNames.entrySet()) {
if (entry.getKey().equals("axis") || entry.getKey().equals("facing") || entry.getKey().equals("half") || entry.getKey().equals("shape") || entry.getKey().equals("open") || entry.getKey().equals("powered") || entry.getKey().equals("stage") || entry.getKey().equals("decayable")) {
Map<List<String>, BlockState> filtered = new HashMap<>();
for (Entry<List<String>, BlockState> offender : fullVariant.entrySet()) {
List<String> key = new ArrayList<>(offender.getKey());
for (String fv : entry.getValue()) {
key.remove(entry.getKey() + "=" + fv);
}
if (!key.isEmpty()) {
filtered.put(key, offender.getValue());
}
}
fullVariant = filtered;
}
if (entry.getValue().size() == 1) {
String singleVariant = entry.getKey() + "=" + entry.getValue().iterator().next();
fullVariant = fullVariant.entrySet().stream().collect(Collectors.toMap(fv -> {
List<String> split = new ArrayList<>(fv.getKey());
split.remove(singleVariant);
return split;
}, Entry::getValue));
}
}
for (Entry<List<String>, BlockState> variant : fullVariant.entrySet()) {
if (variant.getKey().size() > 1) {
System.out.print(e.getKey().getName() + " Has multiple Variants:");
for (String s : variant.getKey()) {
System.out.print(" " + s);
}
System.out.print("\n");
}
}
variants.put(e.getKey(), fullVariant.entrySet().stream().collect(Collectors.toMap(en -> String.join(" ", en.getKey().stream().map(s -> s.split("=")[1]).collect(toList())), Entry::getValue)));
});
/*
for (Entry<ItemType, Map<String, ItemStack>> variant : variants.entrySet())
{
System.out.print(variant.getKey().getName() + ":\n");
for (Entry<String, ItemStack> entry : variant.getValue().entrySet())
{
System.out.print(" " + entry.getKey() + ": " + entry.getValue().getTranslation().get() + "\n");
}
}
*/
return variants;
}
use of org.spongepowered.api.block.BlockState in project core by CubeEngine.
the class MaterialMatcher method itemStack.
public ItemStack itemStack(String name, Locale locale) {
if (name == null) {
return null;
}
Map<String, ItemStack> map = localizedStackMap.get(locale);
if (map != null) {
String match = stringMatcher.matchString(name, map.keySet());
ItemStack itemStack = map.get(match);
if (itemStack != null) {
return itemStack.copy();
}
}
String[] parts = name.toLowerCase(Locale.ENGLISH).split("=");
String[] typeName = parts[0].split("=");
ItemType type = material(typeName[0], locale);
if (type == null) {
return null;
}
if (parts.length > 1) {
String variant = parts[1];
if (type.getBlock().isPresent()) {
Map<String, BlockState> variants = variantMap.get(type.getBlock().get());
if (variants != null) {
String match2 = stringMatcher.matchString(variant, variants.keySet());
if (match2 != null) {
return ItemStack.builder().fromBlockState(variants.get(match2)).quantity(1).build();
}
}
}
}
ItemStack.Builder builder = ItemStack.builder().itemType(type).quantity(1);
if (typeName.length == 2) {
try {
builder.add(Keys.ITEM_DURABILITY, Integer.valueOf(typeName[1]));
} catch (IllegalArgumentException e) {
return null;
}
}
return builder.build();
}
use of org.spongepowered.api.block.BlockState in project LanternServer by LanternPowered.
the class BlockRegistryModule method register.
@Override
public void register(int internalId, BlockType blockType) {
LanternBlockType blockType0 = (LanternBlockType) checkNotNull(blockType, "blockType");
checkState(blockType0.getBlockStateBase().getBlockStates().stream().filter(s -> !((LanternBlockState) s).isExtended()).count() <= 1, "You cannot register a blockType with more then one state with this method.");
register0(internalId, blockType0, blockState -> (byte) 0);
}
Aggregations