Skip to content

Commit 6198c96

Browse files
authored
Merge pull request #7 from ELDEpendenci/develop
Develop
2 parents 1a7b9bc + f501ae1 commit 6198c96

File tree

8 files changed

+59
-13
lines changed

8 files changed

+59
-13
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
env:
2-
version: 0.1.1.1 # 你的版本名稱
2+
version: 0.1.2 # 你的版本名稱
33
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
plugin_name: ELDependenci-MVC-plugin
55

ELDependenci-MVC-plugin/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<artifactId>ELDependenci-MVC-Module</artifactId>
77
<groupId>org.eldependenci</groupId>
8-
<version>0.1.1</version>
8+
<version>0.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>ELDependenci-MVC-plugin</artifactId>
13-
<version>${project.parent.version}.1</version>
13+
<version>${project.parent.version}</version>
1414

1515
<dependencies>
1616
<dependency>

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/ELDGUI.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ private void initReturnTypeManager(ReturnTypeManager returnTypeManager) {
201201

202202
private void initMethodParseManager(MethodParseManager parser) {
203203
parser.registerParser((t, annos) -> t.equals(UISession.class), (annotations, t, e) -> session);
204-
parser.registerParser((t, annos) -> {
204+
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == FromPattern.class), (annotations, t, e) -> {
205+
FromPattern pattern = (FromPattern) Arrays.stream(annotations).filter(a -> a.annotationType() == FromPattern.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find @FromPattern in List<ItemStack> parameters"));
205206
if (t instanceof ParameterizedType) {
206207
var parat = (ParameterizedType) t;
207-
return parat.getActualTypeArguments()[0] == ItemStack.class && parat.getRawType() == List.class;
208+
if (parat.getActualTypeArguments()[0] == ItemStack.class && parat.getRawType() == List.class){
209+
return this.currentView.getEldgContext().getItems(pattern.value());
210+
}
208211
}
209-
return false;
210-
}, (annotations, t, e) -> {
211-
FromPattern pattern = (FromPattern) Arrays.stream(annotations).filter(a -> a.annotationType() == FromPattern.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find @FromPattern in List<ItemStack> parameters"));
212-
return this.currentView.getEldgContext().getItems(pattern.value());
212+
throw new IllegalStateException("@FromPattern 必須使用 List<ItemStack> 作為其類型");
213213
});
214214
//parser.registerParser((t, annos) -> t.equals(UIRequest.class), (annotations, t, e) -> eldgContext);
215215
parser.registerParser((t, annos) -> t.equals(Player.class), (annotations, t, e) -> owner);
@@ -241,6 +241,28 @@ private void initMethodParseManager(MethodParseManager parser) {
241241
LOGGER.debug("using " + toConvert + " to create instance of " + model);
242242
return PersistDataUtils.mapToObject(toConvert, model);
243243
});
244+
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == MapAttribute.class),
245+
(annotations, type, event) -> {
246+
MapAttribute attribute = (MapAttribute) Arrays.stream(annotations).filter(a -> a.annotationType() == MapAttribute.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find MapAttribute annotation"));
247+
var context = this.currentView.getEldgContext();
248+
boolean isMap = false;
249+
if (type instanceof ParameterizedType){
250+
var parat = (ParameterizedType)type;
251+
isMap = parat.getRawType() == Map.class && parat.getActualTypeArguments()[0] == String.class && parat.getActualTypeArguments()[1] == Object.class;
252+
}
253+
254+
if (!isMap) throw new IllegalStateException("@MapAttribute 必須使用 Map<String, Object> 作為其類型");
255+
Map<String, Object> fieldMap = context.getItems(attribute.value())
256+
.stream()
257+
.filter(item -> context.getAttribute(item, AttributeController.FIELD_TAG) != null)
258+
.collect(Collectors
259+
.toMap(
260+
item -> context.getAttribute(item, AttributeController.FIELD_TAG),
261+
item -> Optional.ofNullable(context.getAttribute(item, AttributeController.VALUE_TAG)).orElseThrow(() -> new IllegalStateException("The value tag of " + item.toString() + " is null."))
262+
)
263+
);
264+
return PersistDataUtils.toNestedMap(fieldMap);
265+
});
244266
}
245267

246268
private ItemStack getItemByEvent(InventoryEvent e) {

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/PersistDataUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
142142
}
143143
field.setAccessible(true);
144144
Object value = map.get(field.getName());
145-
if (value instanceof Map<?, ?>){
145+
if (value instanceof Map<?, ?> && field.getType() != Map.class){
146146
Map<String, Object> m = (Map<String, Object>) value;
147147
value = mapToObject(m, field.getType());
148148
}

ELDependenci-MVC-plugin/src/main/java/com/ericlam/mc/eldgui/demo/test/TestController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ericlam.mc.eldgui.demo.test;
22

3+
import com.ericlam.mc.eldgui.controller.MapAttribute;
34
import com.ericlam.mc.eldgui.controller.ModelAttribute;
45
import com.ericlam.mc.eldgui.controller.UIController;
56
import com.ericlam.mc.eldgui.event.ClickMapping;
@@ -8,6 +9,8 @@
89
import com.ericlam.mc.eldgui.view.BukkitView;
910
import org.bukkit.entity.Player;
1011

12+
import java.util.Map;
13+
1114
@UIController("test")
1215
public class TestController {
1316

@@ -22,8 +25,9 @@ public void beforeCreate(Player player){
2225

2326

2427
@ClickMapping(view = TestView.class, pattern = 'A')
25-
public void onClick(@ModelAttribute('Z') TestModel test, Player player){
28+
public void onClick(@ModelAttribute('Z') TestModel test, Player player, @MapAttribute('Z') Map<String, Object> map){
2629
player.sendMessage(test.toString());
30+
player.sendMessage(map.toString());
2731
}
2832

2933

eldependenci-mvc/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>ELDependenci-MVC-Module</artifactId>
77
<groupId>org.eldependenci</groupId>
8-
<version>0.1.1</version>
8+
<version>0.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ericlam.mc.eldgui.controller;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* 獲取指定 Pattern 的 所有數值,必須需要使用 {@code Map<String, Object>} 來裝載。
10+
*/
11+
@Target(ElementType.PARAMETER)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface MapAttribute {
14+
15+
/**
16+
*
17+
* @return 指定 pattern
18+
*/
19+
char value();
20+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>org.eldependenci</groupId>
88
<artifactId>ELDependenci-MVC-Module</artifactId>
99
<packaging>pom</packaging>
10-
<version>0.1.1</version>
10+
<version>0.1.2</version>
1111
<modules>
1212
<module>eldependenci-mvc</module>
1313
<module>ELDependenci-MVC-plugin</module>

0 commit comments

Comments
 (0)