Skip to content

Commit

Permalink
extended counter datatypes support (needs testing)
Browse files Browse the repository at this point in the history
entity slain counter
  • Loading branch information
crashdemons committed Apr 19, 2020
1 parent eaf553d commit 29fb0cf
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 325 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.crashdemons</groupId>
<artifactId>LoreKillCounter</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
179 changes: 98 additions & 81 deletions src/main/java/com/github/crashdemons/lorekillcounter/Counter.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,98 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/ .
*/
package com.github.crashdemons.lorekillcounter;

import org.bukkit.ChatColor;

/**
*
* @author crashdemons (crashenator at gmail.com)
*/
public class Counter {
private int count=0;
private CounterType type=CounterType.INVALID;

public String toStringFormatted(){
return ChatColor.GREEN+getName()+": "+ChatColor.RED+getCount();
}

public String toString(){
return getName()+": "+getCount();
}

public String getName(){
return type.getDisplayName();
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public CounterType getType() {
return type;
}

public void setType(CounterType type) {
this.type = type;
}
public boolean isValid(){ return type!=CounterType.INVALID; }

public Counter(CounterType type){
this.type=type;
}
public Counter(CounterType type, int count){
this.type=type;
this.count=count;
}

public void increment(){
this.count++;
}

public static Counter fromLoreLine(String line){
line = ChatColor.stripColor(line);
String[] pieces = line.split(": ");
//System.out.println(pieces.length);
//System.out.println(pieces[0]);
if(pieces.length!=2) return null;//not the right number of colons

//parse the string to get the name/count
String name = pieces[0];
String str_count = pieces[1];
int count;
try{
count = Integer.parseInt(str_count);
}catch(NumberFormatException e){
//System.out.println("Invalid integer value "+str_count);
return null;//invalid count string - can't be a counter
}

CounterType type = CounterType.fromDisplayName(name);//match display name
if(type==null) return null;
return new Counter(type,count);
}
}
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/ .
*/
package com.github.crashdemons.lorekillcounter;

import java.text.MessageFormat;
import org.bukkit.ChatColor;

/**
*
* @author crashdemons (crashenator at gmail.com)
*/
public class Counter {
private int count=0;
//private CounterType type=CounterType.INVALID;
private final CounterType type;

public String toStringFormatted(){
return ChatColor.GREEN+getName()+": "+ChatColor.RED+getCount();
}

public String toString(){
return getName()+": "+getCount();
}

public String getName(){
return type.getDisplayName();
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public CounterType getType() {
return type;
}

// public void setType(CounterType type) {
// this.type = type;
// }
public boolean isValid(){ return type.baseType!=CounterBaseType.INVALID; }


public Counter(CounterBaseType type){
this.type = new CounterType(type);
}
public Counter(CounterBaseType type, int count){
this.type = new CounterType(type);
this.count=count;
}

public Counter(CounterType type){
this.type = type;
}
public Counter(CounterType type, int count){
this.type = type;
this.count=count;
}
/*public Counter(CounterType type, String extendedData, int count){
this.type = new ExtendedCounterType(type, extendedData);
this.count=count;
}*/

public void increment(){
this.count++;
}

public static Counter fromLoreLine(String line){
line = ChatColor.stripColor(line);
String[] pieces = line.split(": ");
//System.out.println(pieces.length);
//System.out.println(pieces[0]);
if(pieces.length!=2) return null;//not the right number of colons

//parse the string to get the name/count
String name = pieces[0];
String str_count = pieces[1];
int count;
try{
count = Integer.parseInt(str_count);
}catch(NumberFormatException e){
//System.out.println("Invalid integer value "+str_count);
return null;//invalid count string - can't be a counter
}

EntitySlainCounterType slainCounter = EntitySlainCounterType.fromDisplayName(name);
if(slainCounter!=null) return new Counter(slainCounter, count);
CounterType counterType = CounterType.fromDisplayName(name);
if(counterType!=null) return new Counter(counterType, count);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/ .
*/
package com.github.crashdemons.lorekillcounter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

/**
*
* @author crashdemons (crashenator at gmail.com)
*/
public enum CounterBaseType {
INVALID("Invalid Counter"),
PLAYER_KILLS("Player Kills", "pk","players","pvp"),
MOB_KILLS("Mob Kills", "mk","mobs","pve"),

PLAYER_HEADS("Players Beheaded","pheads","ph"),
MOB_HEADS("Mobs Beheaded","mheads","mh"),


ORES_MINED("Ores Mined","ores","om"),

ENTITIES_SLAIN(true, "{0}s Slain", "entities","es","ek"),

;


private final String displayName;
private final boolean extended;//whether the counter type requires extended information (like entity type) to be functional

private static class Lookups {
static final HashMap<String, CounterBaseType> shortNames = new HashMap<>();
}

CounterBaseType(boolean isExtendedType, String displayName, String... shortNames){
this.displayName=displayName;
this.extended = isExtendedType;
for(String shortName : shortNames){
Lookups.shortNames.put(shortName.toUpperCase(), this);
}
}

CounterBaseType(String displayName, String... shortNames){
this.displayName=displayName;
this.extended = false;
for(String shortName : shortNames){
Lookups.shortNames.put(shortName.toUpperCase(), this);
}
}

public boolean isExtended(){
return extended;
}

public String getDisplayName(){
if(displayName==null) return this.name();
return displayName;
}

public static CounterBaseType fromDisplayName(String name){
//determine the type of counter on the item
for(CounterBaseType type : CounterBaseType.values()) //check all counters for a matching name
if(type.getDisplayName().equals(name)) //the name of the counter matches this item
return type;
return null;
}

public static CounterBaseType fromShortName(String name){
CounterBaseType result = Lookups.shortNames.get(name.toUpperCase());
if(result!=null) return result;
try{
return CounterBaseType.valueOf(name.toUpperCase());
}catch(Exception e){
return null;
}
}

public CounterType createType(){
return new CounterType(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
*/
package com.github.crashdemons.lorekillcounter;

import static com.github.crashdemons.lorekillcounter.CounterBaseType.MOB_HEADS;
import static com.github.crashdemons.lorekillcounter.CounterBaseType.MOB_KILLS;
import static com.github.crashdemons.lorekillcounter.CounterBaseType.ORES_MINED;
import static com.github.crashdemons.lorekillcounter.CounterBaseType.PLAYER_HEADS;
import static com.github.crashdemons.lorekillcounter.CounterBaseType.PLAYER_KILLS;
import com.sun.istack.internal.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -103,4 +114,56 @@ public static boolean incrementMatchingCounters(Player player, List<CounterType>

return CounterManager.applyCounterOperation(player,createIncrementMatchOperation(types));
}




//------------------------------------------------------------

public static CounterType typeFromDisplayName(String name){
//determine the type of counter on the item
CounterType type = EntitySlainCounterType.fromDisplayName(name);
if(type==null) type = CounterType.fromDisplayName(name);
return type;
}

public static CounterType typeFromShortName(String name){
return new CounterType(CounterBaseType.fromShortName(name));
}

private static boolean isOre(@NotNull Material m){
String typeName = m.toString().toUpperCase();
if(typeName.endsWith("_ORE")) return true;
if(typeName.equals("ANCIENT_DEBRIS")) return true;
return false;
}

public static List<CounterType> typeFromBlockBreak(Block b){
ArrayList<CounterType> types = new ArrayList<>();
Material mat = b.getType();
if(mat==null || mat==Material.AIR) return types;//no mining of nothing please!
if(isOre(mat)) types.add(ORES_MINED.createType());

return types;
}


public static List<CounterType> typeFromEntityDeath(Entity e){
ArrayList<CounterType> types = new ArrayList<>();
if(!(e instanceof LivingEntity)) return types;
if(e instanceof ArmorStand) return types;

EntityType type = e.getType();//notnull
types.add(new EntitySlainCounterType(type));
if(e instanceof Player) types.add(PLAYER_KILLS.createType());
else types.add(MOB_KILLS.createType());
return types;
}

public static CounterType typeFromEntityHeadDrop(Entity e){
if(!(e instanceof LivingEntity)) return null;
if(e instanceof ArmorStand) return null;
if(e instanceof Player) return PLAYER_HEADS.createType();
return MOB_HEADS.createType();
}
}
Loading

0 comments on commit 29fb0cf

Please sign in to comment.