-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
groovier-plugin/src/test/groovy/com/ericlam/mc/groovytest/TestGroovyDelegate.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.ericlam.mc.groovytest | ||
|
||
import org.bukkit.Material | ||
import org.junit.jupiter.api.Test | ||
|
||
class TestGroovyDelegate { | ||
|
||
@Test | ||
void testGroovyDelegate() { | ||
def builder = new ItemBuilderImpl() as ItemBuilder | ||
builder.item = new Item() | ||
builder.meta = builder.item.meta | ||
|
||
builder.with { | ||
type = Material.DIAMOND | ||
displayName = "Hello Diamond!" | ||
lore = ["Hello", "Diamond!"] | ||
} | ||
|
||
println builder.item.toString() | ||
} | ||
|
||
|
||
trait ItemBuilder { | ||
@Delegate Item item | ||
@Delegate ItemMeta meta | ||
} | ||
|
||
class ItemBuilderImpl implements ItemBuilder { | ||
ItemBuilderImpl(){ | ||
this.item = new Item() | ||
this.meta = item.meta | ||
} | ||
} | ||
|
||
|
||
class Item { | ||
private Material type = Material.STONE | ||
private ItemMeta meta = new ItemMeta() | ||
|
||
void setType(Material type) { | ||
this.type = type | ||
} | ||
|
||
ItemMeta getMeta() { | ||
return meta | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Item{" + | ||
"type=" + type + | ||
", meta=" + meta + | ||
'}'; | ||
} | ||
} | ||
|
||
class ItemMeta { | ||
private String displayName; | ||
private List<String> lore; | ||
|
||
void setDisplayName(String displayName) { | ||
this.displayName = displayName | ||
} | ||
|
||
void setLore(List<String> lore) { | ||
this.lore = lore | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "ItemMeta{" + | ||
"displayName='" + displayName + '\'' + | ||
", lore=" + lore + | ||
'}'; | ||
} | ||
} | ||
} |